-
Notifications
You must be signed in to change notification settings - Fork 10
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
Rfc/issue 952 data loading strategies #1157
Rfc/issue 952 data loading strategies #1157
Conversation
} | ||
} | ||
|
||
export async function loader(request) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another thought here is to just lean into the class
based paradigm instead of creating a custom API, e.g. loaders
. So basically something like this, wherein the request
comes through the constructor
instead, any synchronous operations are done there, and let all the async
work happen in connectedCallback
export default class PostPage extends HTMLElement {
constructor(request) {
super();
const params = new URLSearchParams(request.url.slice(request.url.indexOf('?')));
this.postId = params.get('id');
}
async connectedCallback() {
const { postId } = this;
const post = await fetch(`https://jsonplaceholder.typicode.com/posts/${postId}`).then(resp => resp.json());
const { id, title, body } = post;
this.innerHTML = `
<h1>Fetched Post ID: ${id}</h1>
<h2>${title}</h2>
<p>${body}</p>
`;
}
}
We can even call them WSCs (Web Server Components) ™️ 🤠
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per the last Greenwood weekly meeting, a good suggestion was made that the default behavior could be done as in the snippet above, and that possibly with #880, for users that want to opt-in to hydration, that mechanism could be to export a loader
function.
This would have a few benefits:
- It helps draw a line between a basic component definition, and a custom bespoke API
- For hydration, we need to know the props. Having a
loader
function allows us to serialize the props for further hydration at runtime. - Perhaps to tie it all in together, we can leverage apply isomorphic standards to userland libraries and plugins (
globalThis
) #1005 ?
b68732c
to
e258bdd
Compare
> _**Note**: Keep in mind that for these "page" components, you will likely want to _avoid_ Declarative Shadow for rendering the top level (to avoid wrapping static content in `<template>` tags), but definitely use Declarative Shadow DOM within any dependent custom elements of your page._ | ||
A couple of notes: | ||
- WSCs run only on the server, thus you have full access to any APIs of the runtime, with the ability to perform one time `async` operations for [data loading](/docs/server-rendering/#data-loading) in `connectedCallback`. | ||
- In the above example, card.js will automatically be bundled for you on the client side! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... goes Greenwood actually do this? 🤔
I feel like yes, but in reality I don't think I've observed it? Can't say I've directly looked for it since I didn't think it was actually a feature 😅
packages/cli/src/lifecycles/serve.js
Outdated
@@ -293,9 +293,9 @@ async function getHybridServer(compilation) { | |||
const isApiRoute = manifest.apis.has(url.pathname); | |||
const request = transformKoaRequestIntoStandardRequest(url, ctx.request); | |||
|
|||
if (!config.prerender && matchingRoute.isSSR && !matchingRoute.data.static) { | |||
if (!config.prerender && matchingRoute.isSSR && !matchingRoute.prerender) { | |||
console.log('MATCHING ROUTE -> ', matchingRoute.filename); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to delete this console line
packages/plugin-graphql/README.md
Outdated
@@ -1,23 +1,15 @@ | |||
# @greenwood/plugin-graphql | |||
|
|||
## Overview | |||
A plugin for Greenwood to support using [GraphQL](https://graphql.org/) to query Greenwood's content graph. It runs [**apollo-server**](https://www.apollographql.com/docs/apollo-server/) on the backend and provides an [**@apollo/client** _"like"_](https://www.apollographql.com/docs/react/api/core/ApolloClient/#ApolloClient.readQuery) interface for the frontend that you can use. | |||
A plugin for Greenwood to support using [GraphQL](https://graphql.org/) to query Greenwood's [content graph](https://www.greenwoodjs.io/docs/data/) with our optoinal [pre-made queries](https://www.greenwoodjs.io/docs/menus/). It runs [**apollo-server**](https://www.apollographql.com/docs/apollo-server/) on the backend at build time and provides a **"read-only"** [**@apollo/client** _"like"_](https://www.apollographql.com/docs/react/api/core/ApolloClient/#ApolloClient.readQuery) interface for the frontend that you can use. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spelling mistake here on optional
packages/plugin-graphql/README.md
Outdated
@@ -1,23 +1,15 @@ | |||
# @greenwood/plugin-graphql | |||
|
|||
## Overview | |||
A plugin for Greenwood to support using [GraphQL](https://graphql.org/) to query Greenwood's content graph. It runs [**apollo-server**](https://www.apollographql.com/docs/apollo-server/) on the backend and provides an [**@apollo/client** _"like"_](https://www.apollographql.com/docs/react/api/core/ApolloClient/#ApolloClient.readQuery) interface for the frontend that you can use. | |||
A plugin for Greenwood to support using [GraphQL](https://graphql.org/) to query Greenwood's [content graph](https://www.greenwoodjs.io/docs/data/) with our optoinal [pre-made queries](https://www.greenwoodjs.io/docs/menus/). It runs [**apollo-server**](https://www.apollographql.com/docs/apollo-server/) on the backend at build time and provides a **"read-only"** [**@apollo/client** _"like"_](https://www.apollographql.com/docs/react/api/core/ApolloClient/#ApolloClient.readQuery) interface for the frontend that you can use. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also a little funky grammar here at the end of the last sentence.
|
||
> _We are working on re-evaluating and improving our [data loading](https://github.com/ProjectEvergreen/greenwood/issues/952) and [rendering strategies](https://github.com/ProjectEvergreen/greenwood/issues/951) as part of our [1.0 release](https://github.com/ProjectEvergreen/greenwood/milestone/3)._ | ||
As of now, this plugin requires some form of [prerendering](https://www.greenwoodjs.io/docs/server-rendering/#render-vs-prerender) either through: | ||
1. Enabling [custom imports](https://www.greenwoodjs.io/docs/server-rendering/#custom-imports) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clarify that custom imports is related to SSR
4ebfb81
to
806e9e9
Compare
* add support for SSR page loader with runtime request object * refactor static export from getFrontmatter to export const prerender * document loaders and prerendering for SSR routes * adopt and document constructor props pattern for SSR page data loading * update develop SSR test case for constructor props * remove desribe.only * refactor graphql plugin for ESM compat * add test case for experimental prerendering with custom .gql imports * upgrade website for breaking changes * update website documentation and graphql plugin package README * add test cases for adapter plugins and SSR constructor props * upgrade wcc to 0.9.0 * misc PR cleanup
Related Issue
resolves #952
Summary of Changes
constructor props for SSR pages request time data (akaasync loader
functiongetServerSideProps
)getFrontmatter.static
->export const prerender = true
(akagetStaticProps
)prerender
vs SSR vs CSR) #951Web Server Components (WSCs) andloader
export const prerender
prerender: true
/ custom imports alternative to PuppeteerTODO
getStaticPaths
- I think this depends on landing Server side routes should support dynamic route naming (with prerendering) #882 , so will need to track there insteadThoughts / Questions
loaders
pattern vs RSC flavor page components? - https://github.com/ProjectEvergreen/greenwood/pull/1157/files#r1349748857imports
field toresources
in frontmatter for graphpath
->url
(instance of URL)Rethink SSR pages API (- will save this for Layouts (Templates) and Pages #955getFrontmatter
,getTemplates
,getBody
, etc)context plugins as part of SSR (for pages and templates work?)- tracking in Layouts (Templates) and Pages #955refactor context, move all things into manifest.json- tracking in Layouts (Templates) and Pages #955