You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Coming out of #952 , where in we introduced Web Server Components, that took in an instance of Request as a constructor prop
exportdefaultclassPostPageextendsHTMLElement{constructor(request){super();constparams=newURLSearchParams(request.url.slice(request.url.indexOf('?')));this.postId=params.get('id');}asyncconnectedCallback(){const{ postId }=this;constpost=awaitfetch(`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 would want to make sure we could compliment this with the ability to "customize" the response, in addition the already provided HTML. This would be useful for pages, especially in the case of response headers, for setting things like:
Cookies (e.g. Set-Cookie)
Cache Headers
Custom Headers
In addition, being able to customize constructor props would be nice, to encompass all that data fetching / massaging outside of the component definition, where things could awkward within the limitation of a synchronous constructor and could make unit testing easier through dependency injection.
Details
As seen in #880, I think a custom "loader" function could be used for a double-purpose here, in that the loader function can be a mechanic for hydration, in that the user can return some JSON serialiaziable data, and we can inline that into the HTML of the page as well as pass it in as a custom constructor prop.
exportdefaultclassPostPageextendsHTMLElement{constructor({ post }){super();this.post=post;}asyncconnectedCallback(){const{ id, title, body }=this.post;this.innerHTML=` <h1>Fetched Post ID: ${id}</h1> <h2>${title}</h2> <p>${body}</p> `;}}exportasyncfunctionloader(request){constparams=newURLSearchParams(request.url.slice(request.url.indexOf('?')));constpostId=params.get('id');constpost=awaitfetch(`https://example.com/posts/${postId}`).then(resp=>resp.json());return{ post };}
From there, perhaps we can also define an instance of Response to be returned, and then we can just extract / merge everything but the body, so that something like this could be possible?
exportasyncfunctionloader(request){constparams=newURLSearchParams(request.url.slice(request.url.indexOf('?')));constpostId=params.get('id');constpost=awaitfetch(`https://example.com/posts/${postId}`).then(resp=>resp.json());return{props: { post },response: newResponse('',{headers: newHeaders({'Cache-Control': 'max-age=604800'})})};}
I'm not quite sure how to reconcile this with #880 where all we care about is the custom prop, so not sure we can get this all from a Response, or maybe we can just pass in a mutable reference to the response being managed by Greenwood? 🤔
exportasyncfunctionloader(request,response){constparams=newURLSearchParams(request.url.slice(request.url.indexOf('?')));constpostId=params.get('id');constpost=awaitfetch(`https://example.com/posts/${postId}`).then(resp=>resp.json());response.headers.set('Cache-Control','max-age=604800');return{ post },}
The pilot for this can be Lit's SSR hydration support added in #1201
The text was updated successfully, but these errors were encountered:
Summary
Coming out of #952 , where in we introduced Web Server Components, that took in an instance of
Request
as a constructor propWe would want to make sure we could compliment this with the ability to "customize" the response, in addition the already provided HTML. This would be useful for pages, especially in the case of response headers, for setting things like:
Set-Cookie
)In addition, being able to customize constructor props would be nice, to encompass all that data fetching / massaging outside of the component definition, where things could awkward within the limitation of a synchronous constructor and could make unit testing easier through dependency injection.
Details
As seen in #880, I think a custom "loader" function could be used for a double-purpose here, in that the
loader
function can be a mechanic for hydration, in that the user can return some JSON serialiaziable data, and we can inline that into the HTML of the page as well as pass it in as a custom constructor prop.From there, perhaps we can also define an instance of
Response
to be returned, and then we can just extract / merge everything but the body, so that something like this could be possible?I'm not quite sure how to reconcile this with #880 where all we care about is the custom prop, so not sure we can get this all from a
Response
, or maybe we can just pass in a mutable reference to theresponse
being managed by Greenwood? 🤔The pilot for this can be Lit's SSR hydration support added in #1201
The text was updated successfully, but these errors were encountered: