renderer: tree-shake unused rendu context helpers in production templates
メンテナーはふだん 2 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
- issue の種類
- リファクタリング
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- typescript
- 領域
- backend, build-system
調査の方向性
src/build/virtual/renderer-template.ts から始めて renderer-template.dev.ts と比較し、必要な rendu リリースに compileTemplateToModule() が含まれていることを確認します。production renderer のパスで fetch、serverFetch、fetchViteEnv にアクセスできるか確認します。production templates が使用するコンテキストだけを import し、serverFetch と fetch へのアクセスをテストでカバーできれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Summary
The production renderer template (src/build/virtual/renderer-template.ts) compiles the HTML template when Nitro builds, then emits import { renderToResponse } from 'rendu'. renderToResponse always creates the full render context, so the output always includes rendu's cookie utilities (cookie-es parse/serialize plus the lazy $COOKIES proxy), even when the template never uses $COOKIES or setCookie.
Proposed change
rendu is adding a build-time codegen API, compileTemplateToModule(). It generates an ES module that imports and passes in only the render context variables the template actually uses. There is also a lower-level renderContextToResponse() with individually importable createRenderResponse / createRenderURL / createCookies / createSetCookie / createRedirect helpers.
This API is not released yet. The Nitro change is blocked until a rendu release includes it.
Bundling generated modules with rolldown against rendu's dist, with srvx left out:
| Case | Size | Cookie code |
|---|---|---|
Template using only {{ title }} and $URL |
~3.0 KB | no |
Template using setCookie and $COOKIES |
~9.2 KB | yes |
Current renderToResponse import |
~8.5 KB | always |
The production branch of renderer-template.ts would become roughly:
import { hasTemplateSyntax, compileTemplateToModule } from "rendu";
const isVite = nitro.options.builder === "vite";
const renderModule = compileTemplateToModule(html, {
contextKeys: ["fetch", "serverFetch", ...(isVite ? ["fetchViteEnv"] : [])],
});
return /* js */ `
${renderModule}
import { fetch, serverFetch } from 'nitro/app'
${isVite ? `import { fetchViteEnv } from "nitro/vite/runtime"` : ""}
const context = { fetch, serverFetch${isVite ? ", fetchViteEnv" : ""} }
export const rendererTemplate = (request) => render(request, context)
`;
The dev handler (renderer-template.dev.ts) can keep using compileTemplate + renderToResponse, since bundle size doesn't matter there.
Related bug (likely)
Today the production template is compiled with contextKeys: [...RENDER_CONTEXT_KEYS], but the context also contains fetch, serverFetch and (with Vite) fetchViteEnv. In contextKeys mode a compiled template can only see the listed names. So serverFetch / fetchViteEnv in a production template probably throw a ReferenceError, and fetch falls back to globalThis.fetch instead of Nitro's fetch. This is reasoned from the code and not reproduced in a running Nitro app. Dev works because it uses with() mode. Passing these names as contextKeys (as in the snippet above) fixes it.
Tasks
- Release rendu with
compileTemplateToModule - Switch the production renderer template to
compileTemplateToModule - Add a test for
serverFetch/fetchaccess in production renderer templates
- 主要言語
- TypeScript
- スター
- 11.5k
- フォーク
- 968
- 平均マージ
- 4日 9時間
- マージ済み PR(30日)
- 64
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
nitrojs/nitro のほかの issue
-
pending triage
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
メンテナーはふだん 2 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 95/100
メンテナーはふだん 2 日以内に返信
-
cloudflare: `nitro deploy` fails when a parent directory has a Wrangler config対応中かも @draarivpatel-ui が 13 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
メンテナーはふだん 2 日以内に返信
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 2 日以内に返信
-
pending triage
難易度 5/5 1週間以上 初心者へのやさしさ 30/100
nitrojs/nitro#5205 · リアクション 1 件 ·
メンテナーはふだん 2 日以内に返信
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 95/100
amosblomqvist/learnharness#1141 ·
メンテナーはふだん 4 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
ranxianglei/billion-context#8319 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
tencent-connect/dsh-qqbot#148 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
JimLiu/open-artifact#126 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100