Skip to content
This repository was archived by the owner on May 19, 2025. It is now read-only.

Commit 10cfde2

Browse files
authored
fix: do not use development bundling urls in non development context (#98)
* fix: do not use development bundling urls in non development context * test: fix tests
1 parent 0fee517 commit 10cfde2

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

lib/plugin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ export default fp(
114114
await f.register(compressionPn, { enabled: compression });
115115
await f.register(errorsPn);
116116
await f.register(assetsPn, { base, cwd });
117-
await f.register(bundlerPn, { cwd });
117+
await f.register(bundlerPn, { cwd, development });
118118
await f.register(exceptionsPn, { grace, development });
119119
await f.register(hydratePn, { appName: name, base: assetBase, development, prefix });
120120
await f.register(csrPn, { appName: name, base: assetBase, development, prefix });
121-
await f.register(ssrPn, { appName: name, base, prefix });
121+
await f.register(ssrPn, { appName: name, base, prefix, development });
122122
await f.register(importElementPn, { appName: name, development, plugins, cwd });
123123
await f.register(localePn, { locale, cwd });
124124
await f.register(metricsPn);

plugins/bundler.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ const build = async ({ entryPoints = [] } = {}) => {
3131
* @typedef {import("fastify").FastifyRequest<{Params: { "*": string }}>} Request
3232
*/
3333

34-
export default fp(async function bundler(fastify, { cwd = process.cwd() }) {
34+
export default fp(async function bundler(fastify, { cwd = process.cwd(), development = false }) {
35+
if (!development) return;
36+
3537
await fastify.register(etag, { algorithm: "fnv1a" });
3638

3739
fastify.get("/_/dynamic/modules/*", async (/** @type {Request} */ request, reply) => {

plugins/hydrate.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { joinURLPathSegments } from "../lib/utils.js";
55
export default fp(async function hydratePlugin(fastify, { appName = "", base = "/", development = false, prefix = "/" }) {
66
fastify.decorate("hydrate", function hydrate(type, template) {
77
const elementPath = joinURLPathSegments(prefix, `/_/dynamic/files/${type}.js`);
8-
const shadowRootPath = joinURLPathSegments(prefix, `/_/dynamic/modules/@webcomponents/template-shadowroot/template-shadowroot.js`);
8+
let shadowRootPath = `${base}/client/template-shadowroot.js`;
9+
if (development) {
10+
shadowRootPath = joinURLPathSegments(prefix, `/_/dynamic/modules/@webcomponents/template-shadowroot/template-shadowroot.js`);
11+
}
912
// user provided markup, SSR'd
1013
const ssrMarkup = Array.from(ssr(template)).join("");
1114
// polyfill for browsers that don't support declarative shadow dom

plugins/ssr.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { render } from "@lit-labs/ssr";
22
import fp from "fastify-plugin";
33
import { joinURLPathSegments } from "../lib/utils.js";
44

5-
export default fp(async function ssrPlugin(fastify, { appName = "", prefix = "" }) {
5+
export default fp(async function ssrPlugin(fastify, { appName = "", base = "/", prefix = "", development = false }) {
66
fastify.decorate("ssr", function ssr(type, template) {
7-
const shadowRootPath = joinURLPathSegments(prefix, `/_/dynamic/modules/@webcomponents/template-shadowroot/template-shadowroot.js`);
7+
let shadowRootPath = `${base}/client/template-shadowroot.js`;
8+
if (development) {
9+
shadowRootPath = joinURLPathSegments(prefix, `/_/dynamic/modules/@webcomponents/template-shadowroot/template-shadowroot.js`);
10+
}
811
// user provided markup, SSR'd
912
const ssrMarkup = Array.from(render(template)).join("");
1013
// polyfill for browsers that don't support declarative shadow dom

test/plugins/plugins-bundler.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ afterEach(async (t) => {
2525

2626
test("dependency in node_modules folder bundled and served via url request", async (t) => {
2727
const app = fastify({ logger: false });
28-
await app.register(plugin, { cwd: tmp, enabled: true });
28+
await app.register(plugin, { cwd: tmp, development: true });
2929
const address = await app.listen({ port: 0 });
3030
const result = await fetch(`${address}/_/dynamic/modules/test-dep`);
3131
const response = await result.text();
@@ -52,7 +52,7 @@ test("dependency bundled and served via url request: prefixed plugin", async (t)
5252
const app = fastify({ logger: false });
5353
await app.register(
5454
async () => {
55-
await app.register(plugin, { cwd: tmp, enabled: true });
55+
await app.register(plugin, { cwd: tmp, development: true });
5656
},
5757
{ prefix: "/test" }
5858
);
@@ -64,7 +64,7 @@ test("dependency bundled and served via url request: prefixed plugin", async (t)
6464

6565
test("non-existent dependency results in a 404 error", async (t) => {
6666
const app = fastify({ logger: false });
67-
await app.register(plugin, { cwd: tmp, enabled: true });
67+
await app.register(plugin, { cwd: tmp, development: true });
6868
const address = await app.listen({ port: 0 });
6969
const result = await fetch(`${address}/_/dynamic/modules/does-not-exist`);
7070
const response = await result.json();

0 commit comments

Comments
 (0)