Passing data from hapi server to NEXT page

71 Views Asked by At

I have an application using NEXTJS with Hapi as backend server. The application is having below format.


// Hapi server handling
server.route({
    method: ['GET'],
    path: '/hello-world',
    handler: async (
        _request: hapi.Request,
        _response: hapi.ResponseToolkit
    ) => {
        const {
            raw: { req, res },
            payload,
        } = _request;

        return this.app.renderToHTML(req, res, '/hello-world', {
            ...data,
        });
    },
});

// Next page
public static async getInitialProps(ctx: NextPageContext) {
    const { query, req, pathname } = ctx;
    if (!req) {
        return __NEXT_DATA__.props.pageProps;
    }
    const initialData = // Ajax call for retrieving initial data
    return { initialData };
}

In backend perspective (for example, AWS lambda), here we have 2 backend calls. One for loading the page, and second one is for fetching the data from getInitialProps.

Is there any way by which I can avoid 2 calls by populating data on page load itself and passing it to NEXT?

I understand it is possible to send some data as query parameters from hapi to NEXT, but is the right approach to do?

0

There are 0 best solutions below