Skip to content

dynamic runtime layouts for SSR #1248

Description

@thescientist13

Summary

Coming out of #955 / #1212 , one follow up feature to track is supporting dynamic layouts during SSR. So while this has been possible

src/
  layouts/
    blog.js

The layout was executed only once at bundle time which means it was only a one time render.
https://github.com/ProjectEvergreen/greenwood/blob/v0.30.0-alpha.2/packages/cli/src/lifecycles/bundle.js#L243

Details

Our "Web Server Components" pattern combined with #882 would be useful for layouts that want to customize <head> tags from an external service, like a CMS for example.

export default class BlogLayout 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 { title } = post;

    this.innerHTML = `
      <head>
        <title>Greenwood - Blog: ${title}</title>
      </head>
    `;
  }
}

For compatible build outputs (greenwood serve, serverless adapters), the main trick is that I think this this will require bundling layouts into SSR pages like we do for the page itself. Not sure how tricky this will be though. 🤔

Additional Details

We would also want to ensure feature parity with dynamic routing so that params can be passed to layouts, e.g.

src/
  layouts/
    artist.js // make sure params are available here
  pages/
    artist/
      [name].js

This would require supporting passing params in layout utils

The work is there for the CLI and for Lit in this PR, and adding test cases and updating docs


Additionally, would be nice to support dynamic context layouts too, as currently they are hardcoded to only support HTML.
https://github.com/ProjectEvergreen/greenwood/blob/v0.30.0-alpha.2/packages/cli/src/lib/templating-utils.js#L13

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for Feature.

    Projects

    Status
    📋 Backlog

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions