@returns {import('polka').Middleware}
()
| 65 | // required because the static file server ignores trailing slashes |
| 66 | /** @returns {import('polka').Middleware} */ |
| 67 | function serve_prerendered() { |
| 68 | const handler = serve(path.join(dir, 'prerendered')); |
| 69 | |
| 70 | return (req, res, next) => { |
| 71 | let { pathname, search, query } = polka_url_parser(req); |
| 72 | |
| 73 | try { |
| 74 | pathname = decodeURIComponent(pathname); |
| 75 | } catch { |
| 76 | // ignore invalid URI |
| 77 | } |
| 78 | |
| 79 | if (prerendered.has(pathname)) { |
| 80 | return handler(req, res, next); |
| 81 | } |
| 82 | |
| 83 | // remove or add trailing slash as appropriate |
| 84 | let location = pathname.at(-1) === '/' ? pathname.slice(0, -1) : pathname + '/'; |
| 85 | if (prerendered.has(location)) { |
| 86 | if (query) location += search; |
| 87 | res.writeHead(308, { location }).end(); |
| 88 | } else { |
| 89 | void next(); |
| 90 | } |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | /** @type {import('polka').Middleware} */ |
| 95 | const ssr = async (req, res) => { |
no test coverage detected