score:1

it's just one of solutions: create a separate js file (module), where you will export two functions setdata() and getdata() like this:

//////////////////////////////////////////////////
// ./lib/data-manager.js

let promiseresolver = undefined;
const promise = new promise((resolve) => {
  promiseresolver = resolve;
})

export function setdata(data) { promiseresolver(data); }
export function async getdata() { return promise; }

//////////////////////////////////////////////////
// server.js
import { setdata } from "../lib/data-manager"
import koa from "koa";
import router from "koa-router";

const verified_names = {};
const handle = app.getrequesthandler();  // nextjs use this to handle each request

app.prepare().then(async () => {
  const server = new koa();
  const router = new router();
  ...

  // in verifynamefunc, if name is verified, will set verified_names[name] = some_data
  router.get("/verify_name", verifynamefunc);

  router.get("(.*)", async (ctx) => {
    // custom logic starts here: if name is not verified, redirect to /verify_name
    const name = ctx.query.name;
    if (verified_names[name] === undefined) {
        ctx.redirect(`/verify_name?name=${name}`);
    }
    // here: do some external api calls and generate some data
    var custom_data = custom_logic(verified_names[name]);

    setdata(custom_data); // <-- saving data

    // i think here starts rendering the pages (i.e. enters the execution of `_app.js` and pages)
    // question: how to pass the above `custom_data` to _app.js or pages?
    await handle(ctx.req, ctx.res);  
    }
  });

  server.use(router.routes());
  server.listen(...);
  
});

//////////////////////////////////////////////////
// _app.js
import { getdata } from "../lib/data-manager"

class myapp extends app {
  ...
}

myapp.getinitialprops = async ({ ctx }) => {
  const custom_data = await getdata();  // <-- getting data
};


keep in mind that getinitialprops enables server-side rendering in a page and allows you to do initial data population, it means sending the page with the data already populated from the server (see https://nextjs.org/docs/api-reference/data-fetching/getinitialprops). but, as it was recommended to you, maybe you don't need custom server at all

score:2

you can use getinitialprops and do the redirect in __app.tsx if you need to.

https://nextjs.org/docs/api-reference/data-fetching/getinitialprops

also it's rather easy to check if a name is verified in __app.tsx and then to render a different child based on that. i don't think you need an entire custom server for that.

also take a look at https://nextjs.org/blog/next-10-2#routing-based-on-headers-and-query-string-parameters.


Related Query

More Query from same tag