SSR
Can make hybrid
Static Generation is the pre-rendering method that generates the HTML at build time. The pre-rendered HTML is then reused on each request.
Whenever possible if not based on request because CDN
Server-side Rendering is the pre-rendering method that generates the HTML on each request.
frequently update data that changes on user request
Can even just normal clientside render
Static Generation
Static Generation With Data
Can't render page until you fetch external data(DB, api, file system)
Export async ft called getStaticProps
that will run at build time server-side in prod and every request in dev to pass as props
If you have dynamic routes([...slug].ts), then you need to getStaticPaths
so it knows what to pregenerate
Server-side Rendering
To use Server-side Rendering, you need to export getServerSideProps
instead of getStaticProps
from your page.
Called at request time and context contains request specific parameters
Clientside
ReferenceError: window is not defined
If you configure Next.js to run in a SSR configuration and you’re using window
in your code—or one of your packages does—then you’ll have to make Next aware that this package needs to be dynamic
. The reason this error pops up is this: when html is rendered on the server (remember, SSR stands for server-side rendering), there’s no instance of window
, because, well, there’s simply none. window
is only accessible on the client-side.
I ran into this with a package called react-json-view
. I use it to render beautiful json in html. Here’s how to make it work:
Now, simply proceed to use that import as you’ve done before.
Last updated