diff --git a/Dockerfile b/Dockerfile index cd8b433..fa3521a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,11 +13,10 @@ RUN pnpm prune --prod FROM node:20-alpine WORKDIR /app COPY --from=build /app/build /app/build -COPY --from=build /app/server.mjs /app/server.mjs COPY --from=build /app/node_modules /app/node_modules RUN echo '{"type":"module"}' > /app/package.json EXPOSE 3000 ENV NODE_ENV=production ENV HOST=0.0.0.0 -CMD [ "./server.mjs" ] +CMD [ "node", "./build/headplane/server.js" ] diff --git a/server.mjs b/server.mjs index 9173d47..bcf6401 100755 --- a/server.mjs +++ b/server.mjs @@ -55,13 +55,32 @@ const handler = remixRequestHandler(build, 'production') const http = createServer(async (req, res) => { const url = new URL(`http://${req.headers.host}${req.url}`) + if (!url.pathname.startsWith(PREFIX)) { + res.writeHead(404) + res.end() + return + } + + // We need to handle an issue where say we are navigating to $PREFIX + // but Remix does not handle it without the trailing slash. This is + // because Remix uses the URL constructor to parse the URL and it + // will remove the trailing slash. We need to redirect to the correct + // URL so that Remix can handle it correctly. + if (url.pathname === PREFIX) { + res.writeHead(302, { + Location: `${PREFIX}/` + }) + res.end() + return + } + // Before we pass any requests to our Remix handler we need to check // if we can handle a raw file request. This is important for the // Remix loader to work correctly. // // To optimize this, we send them as readable streams in the node // response and we also set headers for aggressive caching. - if (url.pathname.startsWith(`${PREFIX}assets/`)) { + if (url.pathname.startsWith(`${PREFIX}/assets/`)) { const filePath = join(baseDir, url.pathname.replace(PREFIX, '')) const exists = existsSync(filePath) const stats = statSync(filePath) diff --git a/vite.config.ts b/vite.config.ts index 3ab8da1..a0c8cb8 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -6,9 +6,9 @@ import tsconfigPaths from 'vite-tsconfig-paths' installGlobals() -const prefix = process.env.__INTERNAL_PREFIX || '/admin/' -if (!prefix.endsWith('/')) { - throw new Error('Prefix must end with a slash') +const prefix = process.env.__INTERNAL_PREFIX || '/admin' +if (prefix.endsWith('/')) { + throw new Error('Prefix must not end with a slash') } export default defineConfig(({ isSsrBuild }) => { @@ -43,11 +43,11 @@ export default defineConfig(({ isSsrBuild }) => { } return ({ - base: prefix, + base: `${prefix}/`, build: isSsrBuild ? { target: 'ES2022' } : {}, plugins: [ remix({ - basename: prefix, + basename: `${prefix}/`, }), tsconfigPaths(), babel({