Skip to content

[Feature]: Support for preloading assets in dynamically generated HTML for server side rendering #4868

@AjayPoshak

Description

@AjayPoshak

What problem does this feature solve?

I have a custom SSR setup where I generate the custom HTML on every request. Now I can read the "manifest.json" file and read the initital JS and CSS paths and add them to my custom HTML.

I am wondering if its possible to extend the same support for preloading assets.

What does the proposed API look like?

Something similar to what read the initial js and css chunks look like.

async function renderHtmlPage(): Promise<string> {
  const manifest = await fs.promises.readFile('./dist/manifest.json', 'utf-8');
  const { entries } = JSON.parse(manifest);

  const { js, css } = entries['index'].initial;
  const { js: scriptsToPreload, css: stylesToPreload } = entries['index'].preloadHints; // Similar to getting CSS and JS entries of initial chunk

  const scriptTags = js
    .map((url) => `<script src="${url}" defer></script>`)
    .join('\n');
  const styleTags = css
    .map((file) => `<link rel="stylesheet" href="${file}">`)
    .join('\n');

  return `
    <!DOCTYPE html>
    <html>
      <head>
        ${preloadScriptTags}
        ${preloadStyleTags}
        ${scriptTags}
        ${styleTags}
      </head>
      <body>
        <div id="root"></div>
      </body>
    </html>`;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions