renderer: tree-shake unused rendu context helpers in production templates

オープン
#4,622 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
48/100
issue の種類
リファクタリング
明瞭さ
おおむね明確
活発さ
活発
技術スタック
typescript

調査の方向性

src/build/virtual/renderer-template.ts から始めて renderer-template.dev.ts と比較し、必要な rendu リリースに compileTemplateToModule() が含まれていることを確認します。production renderer のパスで fetch、serverFetch、fetchViteEnv にアクセスできるか確認します。production templates が使用するコンテキストだけを import し、serverFetch と fetch へのアクセスをテストでカバーできれば完了です。

索引モデルが issue の本文から書いたものです。

説明

performance
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 / fetch access in production renderer templates
主要言語
TypeScript
スター
11.5k
フォーク
968
平均マージ
4日 9時間
マージ済み PR(30日)
64

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

nitrojs/nitro のほかの issue

nitrojs/nitro の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。