Initial commit
This commit is contained in:
32
landing/src/server/middleware/not-found-status.ts
Normal file
32
landing/src/server/middleware/not-found-status.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { defineEventHandler, getRequestURL, setResponseStatus } from 'h3';
|
||||
|
||||
/**
|
||||
* Answers 404 for addresses this app does not serve.
|
||||
*
|
||||
* The status cannot be set from the catch-all page's `load`: Analog resolves
|
||||
* loaders through an internal request, so anything set on that response never
|
||||
* reaches the browser. Here it can be, and the route table is small enough to
|
||||
* state outright — the landing is one page.
|
||||
*/
|
||||
const KNOWN_PATHS = new Set(['/', '/index.html']);
|
||||
|
||||
const PASSTHROUGH_PREFIXES = ['/api/', '/_analog/', '/assets/', '/@', '/src/', '/node_modules/'];
|
||||
|
||||
export default defineEventHandler((event) => {
|
||||
const { pathname } = getRequestURL(event);
|
||||
|
||||
if (KNOWN_PATHS.has(pathname)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Static files answer for themselves — a missing one is Nitro's 404, not a
|
||||
// page, and stamping this status on a hit would break the asset.
|
||||
if (
|
||||
PASSTHROUGH_PREFIXES.some((prefix) => pathname.startsWith(prefix)) ||
|
||||
/\.[a-z0-9]+$/i.test(pathname)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
setResponseStatus(event, 404);
|
||||
});
|
||||
Reference in New Issue
Block a user