Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next.js SSR example #1596

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft

Next.js SSR example #1596

wants to merge 8 commits into from

Conversation

balegas
Copy link
Contributor

@balegas balegas commented Aug 29, 2024

Items example using SSR.

Load the entire shape on the server and send it down to the client, so that the client gets the initial state of the shape on first response and can resume replication from the tip of the shape stream.

First time with nextjs, I still don't know how some things work. I suppose this is good forn an example. I'm happy to receive feedback from nextjs experts :D

Copy link
Contributor

@KyleAMathews KyleAMathews left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments.

Don't you want to use either https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props or https://nextjs.org/docs/pages/building-your-application/data-fetching/get-static-props here? Right now I don't see that any data is being cached during server rendering.

EDIT: Oh I see... this is RSC stuff

examples/nextjs-ssr-example/app/Home.tsx Outdated Show resolved Hide resolved
examples/nextjs-ssr-example/app/Home.tsx Outdated Show resolved Hide resolved
examples/nextjs-ssr-example/app/shape.tsx Outdated Show resolved Hide resolved
packages/typescript-client/src/client.ts Outdated Show resolved Hide resolved
import { unstable_noStore as noStore } from "next/cache"

const Page = async () => {
noStore()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why (again) no server rendering?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This turns static rendering on the server off. Was the best working solution I've found.

Copy link

netlify bot commented Oct 3, 2024

Deploy Preview for electric-next ready!

Name Link
🔨 Latest commit 2d933e8
🔍 Latest deploy log https://app.netlify.com/sites/electric-next/deploys/66fde0dfcd5fe8000810c281
😎 Deploy Preview https://deploy-preview-1596--electric-next.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.


// Hack to avoid caching behavior in Next.js
// should work just with {cache: `no-store`}
const fetchClient: typeof fetch = (...args) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need to do this hack, despite nextjs documentation. Maybe someone has any idea

const Page = async () => {
noStore()

return <Home shapes={{ items: getSerializedShape(itemShape) }} />
Copy link
Contributor Author

@balegas balegas Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I came back to this example, my naive intention was to use the shapeCache in the React hook to be able to preload a shape in the server and then the client component transparently would load the shape from that same cache (which would have been hydrated in the client).

I didn't find any way of passing any state between server-side rendered page.tsx and the client component Home.tsx that would not use component properties, which is not so transparent. I think this code is quite clean, but I'm not sure if it's possible to do best.

I didn't want to investigate using external storage for that, since I considered that would be out of scope of the example.

Copy link
Contributor

@thruflo thruflo Oct 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how useful this pattern is.

Generally, we can sync data that is available for low latency / edge SSR.

Pushing that data to the client as serialised JSON in the html source seems … a bit old school and bloats the page that you’re trying to accelerate.

With default SSR you have to make sure the data is exactly the same when rendering on the server and the client. Unless we can somehow work around that.

I wonder if it’s better to have the server render and then have the client take over once the data is synced into the client. But only if we can make the pattern work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The client receives the rendered page (with the items) with the response.
That JSON object is used to hydrate the shape when the component renders in the client, which will allow it to resume shape sync at the position that the server was, instead of starting from scratch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants