Ask Your Question
1

Is it possible to utilize Server-side rendering in NextJS 13 in a location other than the recently introduced "app" directory?

asked 2023-02-12 11:00:00 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-08-10 13:00:00 +0000

devzero gravatar image

Yes, it is possible to utilize Server-side rendering (SSR) in NextJS 13 in a location other than the recently introduced "app" directory. NextJS allows developers to configure the location of the file responsible for server-side rendering using the serverRuntimeConfig option in the Next.js configuration file (next.config.js).

For example, if you want to create a custom SSR file named custom-ssr.js in the root directory of your project, you can add the following lines of code to your next.config.js file:

module.exports = {
  serverRuntimeConfig : {
    // Point to the custom SSR file location
    mySsr: './custom-ssr.js',
  },
}

After that, you can use the mySsr key to reference the custom SSR file anywhere in your NextJS application.

import { getServerSideProps } from 'next';
import getConfig from 'next/config';

const { serverRuntimeConfig } = getConfig();

export const getServerSideProps = async () => {
  const { mySsr } = serverRuntimeConfig;
  // Call the custom SSR function
  const result = await mySsr();
  return {
    props: {
      data: result,
    },
  };
};

By using this approach, you can easily organize application files in a way that is more suitable for your project requirements.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-02-12 11:00:00 +0000

Seen: 15 times

Last updated: Aug 10 '21