-
-
Notifications
You must be signed in to change notification settings - Fork 457
RFC - Static Generation / SSG Improvements #355
Description
Problem
Next.js recently released a new API designed to improve Static Site Generation. More details can be found in vercel/next.js#9524.
serverless-next.js
is not currently compatible with the new Next.js API and this RFC proposes changes to support these features.
Proposal
getStaticProps
.
For pages that declares getStaticProps
, Next.js generates a JSON file that holds the return value of executing getStaticProps
. The file is used for client-side routing.
All JSON files for a given build can be found in .next/prerender-manifest.json
.
serverless-next.js
will lookup the files and upload them to S3 so they can be fetch-ed from the browser.
Also, a new cache behaviour will be created in CloudFront for the path pattern /_next/data/*
. This will match any requests made to the JSON files and forward them to S3.
getStaticPaths
When you have a dynamic route you can add a method called getStaticPaths
to the page. The way it works is Next.js generates a fallback
HTML page that can be served when the route has not been pre-rendered on a previous request.
If the path is returned in getStaticPaths
then is pre-rendered at build time. If is not pre-rendered at build time then it needs to be compiled server side in the background the first time the route is visited.
Here is a diagram to illustrate how this will work in serverless-next.js
(fallback: true
):
You can also set fallback: false
. In this case a 404 is returned instead of pre-rendering:
getServerProps
getServerProps
behaves similarly to getInitialProps
. The difference is that on client side transitions, Next.js makes an api request to the server to run getServerProps
and uses that to render the page client side.
In serverless-next.js
this will be handled in the origin-request
edge function as illustrated below:
Preview mode
Preview mode enables you to preview the "draft" of a page at request time rather than build time. Next.js achieves this by always calling getStaticProps
on the page at request time whenever preview mode is enabled. Details of how to enable preview mode and use it can be found in https://nextjs.org/docs/advanced-features/preview-mode.
In serverless-next.js
this will be handled in the origin-request
edge function as illustrated below: