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

Prerender Part 1 & 2: Render landing/marketing/static pages + Private page whileLoading #1641

Merged
merged 115 commits into from
Feb 18, 2021

Conversation

dac09
Copy link
Collaborator

@dac09 dac09 commented Jan 14, 2021

How do you use this?

For pages you'd like to prerender add the prerender prop

      <Route path="/" page={HomePage} name="home" prerender/>
      <Route path="/hello" page={HelloPage} name="hello" prerender/>

In your redwood.toml, add

[web]
  port = 8910
  apiProxyPath = "/.netlify/functions"
+ experimentalPrerender = true

Now when you build your redwood app, it will add an extra build step to prerender your pages.

You can also manually trigger prerendering with

yarn rw prerender --dry-run

If you have an existing app, some changes to your index.js and index.html file (see create-redwood-app template)


So far looking ok on:

  • prerender page, with index.html as the react root
  • bypass App.js (to limit how much we need to change), and only render pages
  • detect all Routes with a prerender prop using modified rw/structure, and passes path to prerenderer
  • bypass cell rendering in prerender mode
  • added a hook useIsBrowser to detect if its in running in the browser, this will become very important (if you've tried some SSG stuff on next/express you probably recognise why). I'm not the biggest fan of this solution though
  • added HoC <BrowserOnly> to bypass prerendering
  • rendering SVG imports
  • typescript components
  • imported css/sass/etc. -> ignores it in prerender
  • useAuth works fine
  • Freely use useMutation, useQuery (even though they won't fetch anything on the server)
  • handles nested paths e.g. /posts/edit -> dist/posts/edit.html
  • handles private pages, by rendering the whileLoading component
  • update create-redwood-app templates to use new file structure with seprate App.js

Todo

  • Update auth generator code to support new App.js file structure
  • Fix: if you import a page in Routes.js it can't prerender.
  • When you run yarn rw build web it doesn't show errors if it fails to prerender. This isn't that bad, because your app will still run. This is limitation of how we're doing Listr tasks I guess?

Need help / still in progress:

  • - rendering whileLoading for Private routes
    After a lot of hacking around with babel and tsmorph (thanks @aldonline) - modifying the router and the routes-autoloader plugin was the correct approach
  • getting babel-plugin-redwood-directory-import to play nice
    Thanks @peterp
  • importing TypeScript pages in the router (is this a general problem, or an issue I just created?)
    unrelated issue, solved elsewhere
  • should we form a list of routes inside the prerender context separately instead of rendering <Routes/>? This feels like a workaround to me, just to make router.routes.homepage() work for example.
    Not relevant anymore, as we're rerendering the whole App
  • remove window from the rest of the codebase, so I can remove the window shim. While the shim works great with just redwood, it can start to show issues on external libraries because they think its running in the browser
    Execution flow is clear of window
    Couple other things to note:

Out of scope for this PR, but important:

…ender-p1

* 'main' of github.com:redwoodjs/redwood:
  v0.23.0
  GH Action PR Comment: Formatting
  add learn.rwjs.com repo to contributors (redwoodjs#1634)
  update PR Package Action formatting
  Updated All-Contributors Data (redwoodjs#1621)
  sync yarn.lock (redwoodjs#1632)
  Resolve promises in the console (redwoodjs#1619)
  Replace prisma deprecated fineOne() -> findUnique() (redwoodjs#1629)
@thedavidprice
Copy link
Contributor

Closes #217

@github-actions
Copy link

github-actions bot commented Jan 15, 2021

📦 PR Packages

Click to Show Package Download Links

https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/create-redwood-app-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-api-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-api-server-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-auth-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-cli-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-core-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-dev-server-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-eslint-config-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-eslint-plugin-redwood-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-forms-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-internal-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-prerender-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-router-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-structure-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-testing-0.25.1-9bda8ef.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/1641/redwoodjs-web-0.25.1-9bda8ef.tgz

Install this PR by running yarn rw upgrade --pr 1641:0.25.1-9bda8ef

@cannikin
Copy link
Member

This is awesome @dac09! Everything sounds good to me.

My initial thought when we talked about getting this working was the prop name static rather than prerender...maybe that's just because I get confused on SSR vs. SSG vs. all the other terms, whereas "static" is a pretty stable name which just means "a plain HTML file" to me. But if this gets re-hydrated after it loads then maybe it's not actually static?

@dac09
Copy link
Collaborator Author

dac09 commented Jan 18, 2021

Thanks @cannikin! Yeah it does hydrate after the page loads, so more like a "partial render", that the browser then enhances to become more dynamic. Still quite a bit to do, but it's taking shape!

Copy link
Contributor

@peterp peterp left a comment

Choose a reason for hiding this comment

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

LGTM. Nicely done @dac09 !

@dac09
Copy link
Collaborator Author

dac09 commented Feb 18, 2021

One more tweak needed :), template variables don't match. then I'll merge

@dac09 dac09 merged commit a78c3cf into redwoodjs:main Feb 18, 2021
dac09 added a commit to dac09/redwood that referenced this pull request Feb 18, 2021
…rib-workspaces

* 'main' of github.com:redwoodjs/redwood:
  Prerender Part 1 & 2: Render landing/marketing/static pages + Private page whileLoading (redwoodjs#1641)
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.

None yet

5 participants