Skip to content

Commit b104000

Browse files
committed
fix service worker cache-control
1 parent e414eeb commit b104000

File tree

14 files changed

+143
-154
lines changed

14 files changed

+143
-154
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ node_modules/
55
.env
66
/index.html
77
/404.html
8+
/sw.js
9+
/sw.js.map
810
/dist/

deno.json

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"imports": {
3-
"@deno/cache-dir": "jsr:@deno/cache-dir@0.13.2",
4-
"@deno/graph": "jsr:@deno/graph@^0.84.0",
5-
"@deno/import-map": "https://deno.land/x/import_map@v0.20.1/mod.ts",
3+
"@deno/cache-dir": "jsr:@deno/cache-dir@0.14.0",
4+
"@deno/graph": "jsr:@deno/graph@^0.86.0",
5+
"@deno/import-map": "https://deno.land/x/import_map@v0.21.0/mod.ts",
66
"@std/datetime": "jsr:@std/datetime@^0.225.0",
77
"@std/encoding": "jsr:@std/encoding@^1.0.0",
88
"@std/fs": "jsr:@std/fs@^1.0.0",
@@ -17,16 +17,12 @@
1717
"zod": "https://deno.land/x/[email protected]/mod.ts"
1818
},
1919
"scopes": {
20-
"https://jsr.io/@deno/cache-dir/0.13.2/": {
21-
"jsr:/@deno/graph@^0.73.1/types": "jsr:@deno/graph@^0.84.0/types",
22-
"jsr:@deno/graph@^0.73.1": "jsr:@deno/graph@^0.84.0"
20+
"https://jsr.io/@deno/cache-dir/0.14.0/": {
21+
"jsr:/@deno/graph@^0.86.0/types": "jsr:@deno/graph@^0.86.0/types",
22+
"jsr:@deno/graph@^0.86.0": "jsr:@deno/graph@^0.86.0"
2323
}
2424
},
2525
"exclude": [
26-
"404.html",
27-
"index.html",
28-
"generated_import_map.json",
29-
"dist",
3026
"static"
3127
],
3228
"lint": {

deno.lock

Lines changed: 46 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

handler.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@ export type Handler<C = unknown> = (
1515

1616
export function toFetch(handler: Handler): (req: Request) => Promise<Response> {
1717
return async (req) => {
18-
const headersOnly = req.method === "HEAD";
19-
if (headersOnly) {
18+
if (req.method === "HEAD") {
2019
req = new Request(req, { method: "GET" });
2120
}
22-
let res = await handler(req, null);
23-
if (headersOnly) {
24-
res = new Response(null, res);
25-
}
26-
return res;
21+
return await handler(req, null);
2722
};
2823
}
2924

main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env -S deno serve --unstable-kv --allow-import=deno.land:443,jsr.io:443 --allow-read=dist,404.html,index.html,robots.txt
1+
#!/usr/bin/env -S deno serve --unstable-kv --allow-import=deno.land:443,jsr.io:443 --allow-read=dist,404.html,index.html,robots.txt,sw.js,sw.js.map
22

33
import { toFetch } from "./handler.ts";
44
import { getHandler } from "./server.ts";

scripts/build.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ const [js, css] = await (async () => {
108108
const currentCacheKey = await createCacheKey(cacheablePaths);
109109
await build({
110110
bundle: true,
111-
outfile: "dist/sw.js",
111+
outfile: "sw.js",
112112
entryPoints: ["static/sw.ts"],
113113
plugins: [denoCachePlugin()],
114114
absWorkingDir: cwd,

server.ts

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,50 @@ import {
1515
import { decodeURLPathComponents, staticFile } from "./static.ts";
1616

1717
export function getHandler(kv: Deno.Kv): Handler {
18-
return logTime(reportHttpErrors(route({
19-
"/": () => staticFile("index.html"),
20-
"/c/:id": () => staticFile("index.html"),
21-
"/robots.txt": () => staticFile("robots.txt"),
22-
"/api/*": route({
23-
"/code": methods({
24-
POST: parseBodyAsJson(Code, async (_, { body: code }) => {
25-
const id = await putCode(kv, code);
26-
return Response.json({ id }, { status: STATUS_CODE.Created });
18+
return logTime(reportHttpErrors(route(
19+
{
20+
"/api/*": route({
21+
"/code": methods({
22+
POST: parseBodyAsJson(Code, async (_, { body: code }) => {
23+
const id = await putCode(kv, code);
24+
return Response.json({ id }, { status: STATUS_CODE.Created });
25+
}),
2726
}),
28-
}),
29-
"/code/:id": methods({
30-
GET: async (_, { params: { id } }) => {
31-
const code = await getCode(kv, id!) ??
32-
fail(new HttpError("Code not found", "NotFound"));
33-
return Response.json({ code });
34-
},
35-
}),
36-
}, () => fail(new HttpError("Not found", "NotFound"))),
37-
}, async (req) => {
38-
const path = decodeURLPathComponents(new URL(req.url).pathname);
39-
if (path) {
40-
try {
41-
return await staticFile(join("dist", ...path), {
42-
cacheControl: "max-age=2592000, immutable",
43-
});
44-
} catch (e) {
45-
if (
46-
!(e instanceof Deno.errors.NotFound ||
47-
e instanceof Deno.errors.NotADirectory ||
48-
e instanceof Deno.errors.IsADirectory)
49-
) {
50-
throw e;
27+
"/code/:id": methods({
28+
GET: async (_, { params: { id } }) => {
29+
const code = await getCode(kv, id!) ??
30+
fail(new HttpError("Code not found", "NotFound"));
31+
return Response.json({ code });
32+
},
33+
}),
34+
}, () => fail(new HttpError("Not found", "NotFound"))),
35+
},
36+
methods({
37+
GET: route({
38+
"/": () => staticFile("index.html"),
39+
"/c/:id": () => staticFile("index.html"),
40+
"/sw.js": () => staticFile("sw.js"),
41+
"/sw.js.map": () => staticFile("sw.js.map"),
42+
"/robots.txt": () => staticFile("robots.txt"),
43+
}, async (req) => {
44+
const path = decodeURLPathComponents(new URL(req.url).pathname);
45+
if (path) {
46+
try {
47+
return await staticFile(join("dist", ...path), {
48+
cacheControl: "max-age=2592000, immutable",
49+
});
50+
} catch (e) {
51+
if (
52+
!(e instanceof Deno.errors.NotFound ||
53+
e instanceof Deno.errors.NotADirectory ||
54+
e instanceof Deno.errors.IsADirectory)
55+
) {
56+
throw e;
57+
}
58+
}
5159
}
52-
}
53-
}
54-
return await staticFile("404.html", { status: STATUS_CODE.NotFound });
55-
})));
60+
return await staticFile("404.html", { status: STATUS_CODE.NotFound });
61+
}),
62+
}),
63+
)));
5664
}

static/app.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @jsxImportSource react */
22

33
import { lazy, type React, Suspense } from "react";
4-
import { Route, Routes } from "react-router-dom";
4+
import { Route, Routes } from "react-router";
55

66
const Home = lazy(() => import("./home.tsx"));
77
const Code = lazy(() => import("./code.tsx"));

static/code.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @jsxImportSource react */
22

33
import { type React, useEffect, useState } from "react";
4-
import { useNavigate, useParams } from "react-router-dom";
4+
import { useNavigate, useParams } from "react-router";
55

66
import { getCode } from "./api.ts";
77
import { ErrorMessage } from "./components/error_message.tsx";

static/deno.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
{
22
"imports": {
33
"@codemirror/commands": "npm:@codemirror/[email protected]",
4-
"@codemirror/language": "npm:@codemirror/[email protected].3",
4+
"@codemirror/language": "npm:@codemirror/[email protected].5",
55
"@codemirror/state": "npm:@codemirror/[email protected]",
6-
"@codemirror/view": "npm:@codemirror/view@6.34.2",
6+
"@codemirror/view": "npm:@codemirror/view@6.35.0",
77
"@primer/octicons-react": "npm:@primer/[email protected]",
88
"@std/assert": "jsr:@std/assert@^1.0.0",
99
"clsx": "npm:[email protected]",
1010
"floating-point-hex-parser": "./deps/floating_point_hex_parser.ts",
1111
"react": "./deps/react.ts",
1212
"react/jsx-runtime": "./deps/react/jsx_runtime.ts",
1313
"react-dom/client": "./deps/react_dom/client.ts",
14-
"react-router-dom": "npm:react-router[email protected]",
14+
"react-router": "npm:react-router@7.0.1",
1515
"use-latest": "./deps/use_latest.ts"
1616
},
1717
"nodeModulesDir": "auto",

0 commit comments

Comments
 (0)