question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use koa.js + next in firebase functions

See original GitHub issue

I want to use firebase functions to deploy the react application for shopify app

I am new to both next and koa

based on this repo the below code is how to host a simple react application in firebase

    const path = require('path')
    const functions = require('firebase-functions')
    const next = require('next')
    
    var dev = process.env.NODE_ENV !== 'production'
    var app = next({
      dev,
      conf: { distDir: `${path.relative(process.cwd(), __dirname)}/next` }
    })
    var handle = app.getRequestHandler()
    
    exports.next = functions.https.onRequest((req, res) => {
      console.log('File: ' + req.originalUrl) // log the page.js file that is being requested
      return app.prepare().then(() => handle(req, res))
    })

Which works correctly, no issue.

Then based on this tutorial from shopify I need to integrate koa and other dependencies in server.js, which in my case I believe it should be placed inside the firebase function. so I get to this code

   const path = require('path')
   const isomorphicFetch = require('isomorphic-fetch');
   const Koa = require('koa');
   const functions = require('firebase-functions')
   const next = require('next');
   const ShopifyConfig = require('./shopify.js');
   
   const { default: createShopifyAuth } = require('@shopify/koa-shopify-auth');
   const dotenv = require('dotenv');
   const { verifyRequest } = require('@shopify/koa-shopify-auth');
   const session = require('koa-session');
   
   dotenv.config();
   
   const port = parseInt(process.env.PORT, 10) || 3000;
   
   var dev = process.env.NODE_ENV !== 'production'
   var app = next({
     dev,
     conf: { distDir: `${path.relative(process.cwd(), __dirname)}/next` }
   })
   var handle = app.getRequestHandler()
   
   const server = new Koa();
   
   server.use(session(server));
   server.keys = [ShopifyConfig.secretKey];
   
   server.use(
     createShopifyAuth({
       apiKey: ShopifyConfig.key,
       secret: ShopifyConfig.secretKey,
       scopes: [],
       afterAuth(ctx) {
         const { shop, accessToken } = ctx.session;
         ctx.redirect('/');
       },
     }),
   );
   
   server.use(verifyRequest());
   
   server.use(async (ctx) => {
     await handle(ctx.req, ctx.res);
     ctx.respond = false;
     ctx.res.statusCode = 200;
   
   });
   
   exports.next = functions.https.onRequest((req, res) => {
     console.log('File: ' + req.originalUrl) // 

     // This is old code
     // return app.prepare().then(() => {
     //   handle(req, res);
     // })

     // I tried this #1
     // server.callback(req, res);
   })

   // I tried this #2
   // exports.next = functions.https.onRequest(server.callback);

   // I tried this #3
   // exports.next = functions.https.onRequest(server.callback());

   // I tried this #4
   exports.next = functions.https.onRequest((req, res) => {
     console.log('File: ' + req.originalUrl) 
   
     return app.prepare().then(() => {
       server.callback(req, res);
       //handle(req, res);
     })
   })

My question is now based on koa what code should be in functions.https.onRequest ?

I tried #1, #2, #3, as well as this post

1 -> I get request timeout

2 -> I get request timeout

3 -> I get Cannot access middleware of undefined

4 -> I get request timeout

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
amardeepsingh20commented, Oct 14, 2019

@kvindasAB Thanks Your solution works

Were you able to get the authentication part working? It seems to keep going in a loop and never authenticate.

createShopifyAuth({
       apiKey: ShopifyConfig.key,
       secret: ShopifyConfig.secretKey,
       scopes: [],
       afterAuth(ctx) {
         const { shop, accessToken } = ctx.session;
         ctx.redirect('/');
       },
     }),

This does not seem to be creating the /auth and /auth/callback routes properly in the firebase cloud functions environment. Any ideas?

2reactions
RezaRahmaticommented, Jun 23, 2019

@kvindasAB Thanks Your solution works

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use koa.js + next in Firebase Functions - Stack Overflow
I want to use Cloud Functions for Firebase to deploy the React application for shopify app. I am new to both Next and...
Read more >
Koa BodyParser with Firebase Cloud Functions ✔️
koa -bodyparser A body parser for Koa, based on co-body. support json, form and text type body.
Read more >
Call functions via HTTP requests | Cloud Functions for Firebase
You can trigger a function through an HTTP request by using functions.https . This allows you to invoke a synchronous function through the...
Read more >
Next.js on Cloud Functions for Firebase with Firebase Hosting
Local development of our Next. js app from the project root. ✔️ next — install firebase function deps, nav to app folder, install...
Read more >
Is Firebase Functions With Node.js Changing Web ... - YouTube
Enroll in my Vue 3 course! Limited time sale! https://course.vuecourse.tech/vue3Firebase functions with Node. js is the future.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found