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

Commit c83dfd5

Browse files
committed
Remove wrong etag header of transformer
1 parent 7ffdce8 commit c83dfd5

File tree

5 files changed

+15
-39
lines changed

5 files changed

+15
-39
lines changed

lib/fs.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export async function getFiles(
6666
/* read source code from fs/cdn/cache */
6767
export async function readCode(
6868
specifier: string,
69-
): Promise<[code: string, mtime: number | undefined, contentType: string]> {
69+
): Promise<[code: string, contentType: string]> {
7070
if (util.isLikelyHttpURL(specifier)) {
7171
const url = new URL(specifier);
7272
if (url.hostname === "esm.sh" && !url.searchParams.has("target")) {
@@ -76,14 +76,11 @@ export async function readCode(
7676
if (res.status >= 400) {
7777
throw new Error(`fetch ${url.href}: ${res.status} - ${res.statusText}`);
7878
}
79-
const val = res.headers.get("Last-Modified");
80-
const mtime = val ? new Date(val).getTime() : undefined;
81-
return [await res.text(), mtime, res.headers.get("Content-Type") || getContentType(url.pathname)];
79+
return [await res.text(), res.headers.get("Content-Type") || getContentType(url.pathname)];
8280
}
8381

8482
specifier = util.splitBy(specifier, "?")[0];
85-
const stat = await Deno.stat(specifier);
86-
return [await Deno.readTextFile(specifier), stat.mtime?.getTime(), getContentType(specifier)];
83+
return [await Deno.readTextFile(specifier), getContentType(specifier)];
8784
}
8885

8986
/* watch the given directory and its subdirectories */

loaders/vue.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export default class VueSFCLoader implements Pick<ModuleLoader, "load"> {
2929
}
3030

3131
async load(pathname: string, env: ModuleLoaderEnv): Promise<ModuleLoaderContent> {
32-
const stat = await Deno.lstat(`.${pathname}`);
3332
const content = await Deno.readTextFile(`.${pathname}`);
3433
const filename = "." + pathname;
3534
const id = (await util.computeHash("SHA-256", filename)).slice(0, 8);
@@ -133,7 +132,6 @@ export default class VueSFCLoader implements Pick<ModuleLoader, "load"> {
133132

134133
return {
135134
code: output.join("\n"),
136-
modtime: stat?.mtime?.getTime(),
137135
lang: isTS ? "ts" : "js",
138136
inlineCSS: css || undefined,
139137
atomicCSS: true,

server/mod.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { ConnInfo, ServeInit } from "https://deno.land/[email protected]/http/server.ts";
22
import { serve as stdServe, serveTls } from "https://deno.land/[email protected]/http/server.ts";
33
import { readableStreamFromReader } from "https://deno.land/[email protected]/streams/conversion.ts";
4-
import { VERSION } from "https://deno.land/x/[email protected]/version.ts";
54
import FetchError from "../framework/core/fetch_error.ts";
65
import type { RouteRecord } from "../framework/core/route.ts";
76
import log, { LevelName } from "../lib/log.ts";
@@ -42,16 +41,6 @@ export const serve = (options: ServerOptions = {}) => {
4241
const jsxConfigPromise = importMapPromise.then(loadJSXConfig);
4342
const moduleLoadersPromise = importMapPromise.then(initModuleLoaders);
4443
const routesPromise = config?.routes ? initRoutes(config.routes) : Promise.resolve({ routes: [] });
45-
const buildHashPromise = Promise.all([jsxConfigPromise, importMapPromise]).then(([jsxConfig, importMap]) => {
46-
const buildArgs = JSON.stringify({
47-
...(config ? util.pick(config, "build", "unocss") : undefined),
48-
comilper: VERSION,
49-
importMap,
50-
jsxConfig,
51-
isDev,
52-
});
53-
return util.computeHash("sha-1", buildArgs);
54-
});
5544
const handler = async (req: Request, connInfo: ConnInfo): Promise<Response> => {
5645
const url = new URL(req.url);
5746
const { host, pathname, searchParams } = url;
@@ -71,15 +60,13 @@ export const serve = (options: ServerOptions = {}) => {
7160
// transform client modules
7261
if (clientModuleTransformer.test(pathname)) {
7362
try {
74-
const [buildHash, jsxConfig, importMap] = await Promise.all([
75-
buildHashPromise,
63+
const [jsxConfig, importMap] = await Promise.all([
7664
jsxConfigPromise,
7765
importMapPromise,
7866
]);
7967
return await clientModuleTransformer.fetch(req, {
8068
importMap,
8169
jsxConfig,
82-
buildHash,
8370
buildTarget: config?.build?.target,
8471
isDev,
8572
});
@@ -100,8 +87,7 @@ export const serve = (options: ServerOptions = {}) => {
10087
const loader = moduleLoaders.find((loader) => loader.test(pathname));
10188
if (loader) {
10289
try {
103-
const [buildHash, jsxConfig, importMap] = await Promise.all([
104-
buildHashPromise,
90+
const [jsxConfig, importMap] = await Promise.all([
10591
jsxConfigPromise,
10692
importMapPromise,
10793
]);
@@ -110,7 +96,6 @@ export const serve = (options: ServerOptions = {}) => {
11096
loaded,
11197
importMap,
11298
jsxConfig,
113-
buildHash,
11499
buildTarget: config?.build?.target,
115100
isDev,
116101
});

server/transformer.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { bundleCSS } from "./bundle_css.ts";
88
import {
99
builtinModuleExts,
1010
getAlephPkgUri,
11+
getDeploymentId,
1112
getUnoGenerator,
1213
regFullVersion,
1314
restoreUrl,
@@ -18,7 +19,6 @@ import { DependencyGraph } from "./graph.ts";
1819
import type { ImportMap, JSXConfig, ModuleLoaderContent } from "./types.ts";
1920

2021
export type TransformerOptions = {
21-
buildHash: string;
2222
buildTarget?: TransformOptions["target"];
2323
importMap: ImportMap;
2424
isDev: boolean;
@@ -33,33 +33,30 @@ export default {
3333
pathname.endsWith(".css");
3434
},
3535
fetch: async (req: Request, options: TransformerOptions): Promise<Response> => {
36-
const { isDev, buildHash, loaded } = options;
36+
const { isDev, loaded } = options;
3737
const { pathname, searchParams, search } = new URL(req.url);
3838
const specifier = pathname.startsWith("/-/") ? restoreUrl(pathname + search) : `.${pathname}`;
3939
const clientDependencyGraph: DependencyGraph | undefined = Reflect.get(globalThis, "clientDependencyGraph");
4040

4141
let sourceCode: string;
42-
let mtime: number | undefined;
4342
let lang: string | undefined;
4443
let isCSS: boolean;
4544
let uno: boolean;
4645
if (loaded) {
4746
sourceCode = loaded.code;
48-
mtime = loaded.modtime;
4947
lang = loaded.lang;
5048
isCSS = loaded.lang === "css";
5149
uno = !!loaded.atomicCSS;
5250
} else {
5351
let codeType: string;
54-
[sourceCode, mtime, codeType] = await readCode(specifier);
52+
[sourceCode, codeType] = await readCode(specifier);
5553
isCSS = codeType.startsWith("text/css");
5654
uno = pathname.endsWith(".jsx") || pathname.endsWith(".tsx");
5755
}
5856

59-
const etag = mtime
60-
? `W/${mtime.toString(16)}-${sourceCode.length.toString(16)}-${buildHash.slice(0, 8)}`
61-
: await util.computeHash("sha-1", sourceCode + buildHash);
62-
if (req.headers.get("If-None-Match") === etag) {
57+
const deployId = getDeploymentId();
58+
const etag = deployId ? `W/${deployId}` : null;
59+
if (etag && req.headers.get("If-None-Match") === etag) {
6360
return new Response(null, { status: 304 });
6461
}
6562

@@ -162,10 +159,10 @@ export default {
162159
resBody = code;
163160
}
164161
}
165-
const headers = new Headers({
166-
"Content-Type": `${resType}; charset=utf-8`,
167-
"Etag": etag,
168-
});
162+
const headers = new Headers([["Content-Type", `${resType}; charset=utf-8`]]);
163+
if (etag) {
164+
headers.set("ETag", etag);
165+
}
169166
if (searchParams.get("v") || (pathname.startsWith("/-/") && regFullVersion.test(pathname))) {
170167
headers.append("Cache-Control", "public, max-age=31536000, immutable");
171168
}

server/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export type ModuleLoaderContent = {
7171
atomicCSS?: boolean;
7272
lang?: "js" | "jsx" | "ts" | "tsx" | "css";
7373
map?: string;
74-
modtime?: number;
7574
};
7675

7776
export { UnoConfig };

0 commit comments

Comments
 (0)