Skip to content
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

feat(server): support load SSR module #2528

Merged
merged 24 commits into from
Jul 1, 2024

Conversation

9aoy
Copy link
Collaborator

@9aoy 9aoy commented Jun 6, 2024

Summary

update server environment api, provide some SSR related APIs for users to use:

type RsbuildDevServer = {
  ...
  environments: {
    [name: string]: {
      ...
      loadBundle: <T>(entryName: string) => Promise<T>;  // Used to load and execute stats bundle.
      getTransformedHtml: (entryName: string) => Promise<string>; //  used to get the compiled HTML template.
    }
  };
};

usage:

import { createRsbuild } from '@rsbuild/core';
import express from 'express';

 export const serverRender = (rsbuild, entryName) => async (req, res, next) => {
+  const { render } = await rsbuild.environments.ssr.loadBundle(entryName);
+
+  const markup = render();
+
+  const template = await rsbuild.environments.web.getTransformedHtml(entryName);
+
+  const html = template.replace(`<!--app-content-->`, markup)
+
+  res.status(200).set({ 'Content-Type': 'text/html' }).send(html)
};

export async function startDevServer() {
  const rsbuild = await createRsbuild({});

  const app = express();

  const rsbuildServer = await rsbuild.createDevServer();

+ app.get('/', serverRender(rsbuild, 'index'))

  app.use(rsbuildServer.middlewares);
  
  ....
}

Related Links

#2221

#2620

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Copy link

netlify bot commented Jun 6, 2024

Deploy Preview for rsbuild ready!

Name Link
🔨 Latest commit 2e50b27
🔍 Latest deploy log https://app.netlify.com/sites/rsbuild/deploys/668279155ef40100088d2565
😎 Deploy Preview https://deploy-preview-2528--rsbuild.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 84 (🔴 down 4 from production)
Accessibility: 97 (no change from production)
Best Practices: 100 (no change from production)
SEO: 92 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@9aoy 9aoy marked this pull request as draft June 6, 2024 07:43
@9aoy 9aoy changed the title WIP: feat(server): support load ssr module feat(server): support load ssr module Jun 28, 2024
@9aoy 9aoy marked this pull request as ready for review June 28, 2024 09:15
@9aoy 9aoy requested a review from chenjiahan June 28, 2024 10:03
e2e/cases/server/ssr/scripts/server.mjs Outdated Show resolved Hide resolved
packages/core/src/server/environment.ts Outdated Show resolved Hide resolved
packages/core/src/server/environment.ts Outdated Show resolved Hide resolved
packages/core/src/server/devServer.ts Show resolved Hide resolved
packages/core/src/server/runner/legacy/asModule.js Outdated Show resolved Hide resolved
packages/shared/src/types/config/dev.ts Outdated Show resolved Hide resolved
packages/core/src/server/runner/type.ts Outdated Show resolved Hide resolved
@chenjiahan chenjiahan changed the title feat(server): support load ssr module feat(server): support load SSR module Jul 1, 2024
@chenjiahan chenjiahan merged commit e11a2a4 into web-infra-dev:main Jul 1, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants