Fix js/css content type (paste)

Update README.md

Signed-off-by: Joe Ma <rikkaneko23@gmail.com>
This commit is contained in:
Joe Ma 2023-03-26 20:52:40 +08:00
parent 7e65c403f7
commit 0f89088486
No known key found for this signature in database
GPG key ID: 7A0ECF5F5EDC587F
3 changed files with 28 additions and 41 deletions

View file

@ -69,54 +69,18 @@ export default {
}
if (path === '/v1' && method == 'GET') {
return await fetch(PASTE_WEB_URL_v1 + '/paste.html', {
cf: {
cacheEverything: true,
},
}).then(value => {
return new Response(value.body, {
// Add the correct content-type to response header
headers: {
'content-type': 'text/html; charset=UTF-8;',
'cache-control': 'public, max-age=172800',
},
});
});
return await proxy_uri(PASTE_WEB_URL_v1 + '/paste.html');
}
if (/\/(js|css)\/.*$/.test(path) && method == 'GET') {
return await fetch(PASTE_WEB_URL + path, {
cf: {
cacheEverything: true,
},
}).then(value => {
return new Response(value.body, {
// Add the correct content-type to response header
headers: {
'content-type': 'text/html; charset=UTF-8;',
'cache-control': 'public, max-age=172800',
},
});
});
return await proxy_uri(PASTE_WEB_URL + path);
}
if (path === '/') {
switch (method) {
// Fetch the HTML for uploading text/file
case 'GET': {
return await fetch(PASTE_WEB_URL + '/paste.html', {
cf: {
cacheEverything: true,
},
}).then(value => {
return new Response(value.body, {
// Add the correct content-type to response header
headers: {
'content-type': 'text/html; charset=UTF-8;',
'cache-control': 'public, max-age=172800',
},
});
});
// Fetch the HTML for uploading text/file
return await proxy_uri(PASTE_WEB_URL + '/paste.html');
}
// Create new paste
@ -636,6 +600,27 @@ function to_human_readable_size(bytes: number): string {
return size;
}
// Proxy URI (limit to html/js/css)
async function proxy_uri(path: string, cf: RequestInitCfProperties = {cacheEverything: true}) {
// Fix content type
let file_type = 'text/plain';
if (path.endsWith('.js')) file_type = 'application/javascript';
if (path.endsWith('.css')) file_type = 'text/css';
if (path.endsWith('.html')) file_type = 'text/html';
return await fetch(path, {
cf,
}).then(value => {
return new Response(value.body, {
// Add the correct content-type to response header
headers: {
'content-type': `${file_type}; charset=UTF-8;`,
'cache-control': 'public, max-age=172800',
},
});
});
}
interface PasteIndexEntry {
title?: string,
mime_type?: string,