Skip to content

Conversation

@hi-ogawa
Copy link
Owner

@hi-ogawa hi-ogawa commented Sep 30, 2025

SSR Assets API

This is a proposal to introduce a new API to allow non-client environment to access assets information commonly required for SSR.

Currently, it is prototyped in my package @hiogawa/vite-plugin-fullstack and it provides import.meta.vite.assets function with a following signature:

function assets({ import?: string, environment?: string }): {
  entry?: string;               // script for <script type="module" src=...>
  js: { href: string, ... }[];  // dependency chunks for <link rel="modulepreload" href=... />
  css: { href: string, ... }[]; // dependency css for <link rel="stylesheet" href=... />
};

The goal of this API is to cover following use cases:

  • for server entry to access client entry
// [server.js] server entry injecting client entry during SSR
function renderHtml() {
  const assets = import.meta.vite.assets({
    entry: "./client.js",
    environment: "client",
  });
  const head = `
    <script type="module" src=${JSON.stringify(assets.entry)}></script>
    <link type="modulepreload" href=${JSON.stringify(assets.js[0].href)}></script>
    ...
  `;
  ...
}
  • for universal route to access assets within its route
    • see examples/react-rotuer and examples/vue-router for concrete examples
// [routes.js] hypothetical router library's routes declaration
export const routes = [
  {
    path: "/about"
    route: () => import("./pages/about.js"),
    routeAssets: mergeAssets(
      import.meta.vite.assets({
        entry: "./pages/about.js",
        environment: "client",
      }),
      import.meta.vite.assets({
        entry: "./pages/about.js",
        environment: "ssr",
      }),
    )
  },
  ...
]
  • server only app to access css
// [server.js]
import "./styles.css" // this will be included in `assets.css` below

function renderHtml() {
  const assets = import.meta.vite.assets({
    // both `import` and `environment` is optional and they are default to current module and environment
    // import: "./server.js",
    // environment: "ssr",
  });
  const head = `
    <link type="stylesheet" href=${JSON.stringify(assets.css[0].href)}></script>
    ...
  `;
  ...
}

TODO

MVP

Future

  • API
    • add client entry dynamically
    • transformIndexHtml on server
    • handle server change event (e.g. reload / refetch)
    • deduplicate client and server css on build when css code split differs
      • treat css on server like client reference?
  • create custom integration of router libraries
    • react router
    • vue router
  • test it on ecosystem framework
    • fresh (server css)

Copy link
Owner Author

hi-ogawa commented Sep 30, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@hi-ogawa hi-ogawa force-pushed the 09-30-feat_fullstack_create_vite-plugin-fullstack branch from 295f341 to 80ec30e Compare September 30, 2025 09:42
@hi-ogawa hi-ogawa force-pushed the 09-30-feat_fullstack_create_vite-plugin-fullstack branch from 841c37c to 0e8b71f Compare September 30, 2025 10:00
@hi-ogawa hi-ogawa force-pushed the 09-30-feat_fullstack_create_vite-plugin-fullstack branch from 6f47bae to f2e28b2 Compare October 1, 2025 03:48
@hi-ogawa hi-ogawa force-pushed the 09-30-feat_fullstack_create_vite-plugin-fullstack branch from 3facbe1 to ee1713f Compare October 1, 2025 05:22
@hi-ogawa hi-ogawa marked this pull request as ready for review October 2, 2025 13:16
@hi-ogawa hi-ogawa force-pushed the 09-30-feat_fullstack_create_vite-plugin-fullstack branch 3 times, most recently from 65c577b to 2651f4d Compare October 3, 2025 01:04
@hi-ogawa hi-ogawa force-pushed the 09-30-feat_fullstack_create_vite-plugin-fullstack branch from 2651f4d to ed06d8a Compare October 3, 2025 01:06
@hi-ogawa hi-ogawa changed the title feat(fullstack): create package feat(fullstack): prototype import.meta.vite.assets API Oct 3, 2025
Copy link
Owner Author

hi-ogawa commented Oct 3, 2025

Merge activity

  • Oct 3, 1:18 AM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Oct 3, 1:18 AM UTC: @hi-ogawa merged this pull request with Graphite.

@hi-ogawa hi-ogawa merged commit c3ba75a into main Oct 3, 2025
4 checks passed
@hi-ogawa hi-ogawa deleted the 09-30-feat_fullstack_create_vite-plugin-fullstack branch October 3, 2025 01:18
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.

2 participants