Pass data from server.js to the next js app and use it globally

Viewed 15
app.prepare().then(() => {
    createServer(async (request, result) => {
        try {
            const parsedUrl = parse(request.url, true);
            const { pathname, query } = parsedUrl;
// ...
            // here config 
            const routes = await serverRouterApiWorker.getRoutes();
            const clonedReactRoutes = Object.assign({}, reactRoutes.routes);
            for (const reactRouteName of Object.keys(clonedReactRoutes)) {
                const reactRoute = clonedReactRoutes[reactRouteName];

                if (routes[reactRouteName] !== undefined && pathname === routes[reactRouteName].path) {
                    await app.render(request, result, reactRoute, query); // Here I choose which path to render
                    delete clonedReactRoutes[reactRouteName];
                }
            }
            
// ...
          
        } catch (err) {
            console.error('Error occurred handling', request.url, err)
            result.statusCode = 500
            result.end('internal server error')
        }
    }).listen(port, (err) => {
        if (err) throw err
        console.log(`> Ready on http://${hostname}:${port}`)
    })
})

I need to get this exact routes config in my next.js application. I need this data to be loaded only when a request is made to the server (reloading the page, opening the page through a browser). If the page opens without reloading the request for new data should not do. I can't use MyApp.getInitialProps because it makes a request every time the page is opened, not just when the page is reloaded. I need something like MyApp.getServerSideProps but I see that it is not possible https://github.com/vercel/next.js/discussions/13199

I would appreciate it if you could show me an example of how to transfer data from server js to application and how to use them (I need to use them in a component)

0 Answers
Related