Skip to content

Commit 31e9bcd

Browse files
committed
check storage
1 parent 48bd7d1 commit 31e9bcd

1 file changed

Lines changed: 27 additions & 7 deletions

File tree

deploy-server.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { listenAndServe } from "https://deno.land/std@0.111.0/http/server.ts";
22
import { MEDIA_TYPES } from './media-type.js';
33

4+
const { sessionStorage, localStorage } = globalThis;
5+
console.log('sessionStorage: ', sessionStorage);
6+
console.log('localStorage: ', localStorage);
7+
48
const assetMap = {
59
'/': './deploy.html',
610
'/README.md': './README.md'
@@ -36,6 +40,11 @@ function removeSlashes(path) {
3640
return removeTrailingSlash(removeLeadingSlash(path));
3741
}
3842

43+
function getFileExtensionFromPath(maidenPathname) {
44+
const [fileExtension] = maidenPathname.split('.').reverse();
45+
return `.${fileExtension}`;
46+
}
47+
3948
async function handler(request) {
4049
const mode = request.headers.get('sec-fetch-mode');
4150
const dest = request.headers.get('sec-fetch-dest');
@@ -44,13 +53,24 @@ async function handler(request) {
4453
const { pathname } = new URL(request.url);
4554
const assetPath = assetMap[pathname];
4655
const maidenPathname = removeSlashes(assetPath);
47-
const [fileExtension] = maidenPathname.split('.').reverse();
4856

49-
console.log('mode: ', mode);
50-
console.log('pathname: ', pathname);
51-
console.log('assetPath: ', assetPath);
52-
console.log('maidenPathname: ', maidenPathname);
53-
console.log('fileExtension: ', fileExtension);
57+
const storage = sessionStorage || localStorage;
58+
59+
if (storage) {
60+
const storedFileKey = removeSlashes(pathname);
61+
const storedFile = storage.getItem(storedFileKey);
62+
63+
if (storedFile) {
64+
return new Response(storedFile, {
65+
// @ts-ignore
66+
headers: {
67+
// @ts-ignore
68+
"content-type": MEDIA_TYPES[getFileExtensionFromPath(storedFileKey)],
69+
"x-cache": 'HIT'
70+
}
71+
});
72+
}
73+
}
5474

5575
const textFileContent = await Deno.readTextFile(assetPath);
5676
// Absolute paths.
@@ -66,7 +86,7 @@ async function handler(request) {
6686
// return new Response(textFileContent);
6787
return new Response(textFileContent, {
6888
headers: {
69-
"content-type": MEDIA_TYPES[`.${fileExtension}`],
89+
"content-type": MEDIA_TYPES[getFileExtensionFromPath(maidenPathname)],
7090
}
7191
});
7292
}

0 commit comments

Comments
 (0)