fix: handle non-prefixed routes on the production server
This commit is contained in:
parent
5c949e2da5
commit
2bc85085f5
@ -13,11 +13,10 @@ RUN pnpm prune --prod
|
|||||||
FROM node:20-alpine
|
FROM node:20-alpine
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY --from=build /app/build /app/build
|
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
|
COPY --from=build /app/node_modules /app/node_modules
|
||||||
RUN echo '{"type":"module"}' > /app/package.json
|
RUN echo '{"type":"module"}' > /app/package.json
|
||||||
|
|
||||||
EXPOSE 3000
|
EXPOSE 3000
|
||||||
ENV NODE_ENV=production
|
ENV NODE_ENV=production
|
||||||
ENV HOST=0.0.0.0
|
ENV HOST=0.0.0.0
|
||||||
CMD [ "./server.mjs" ]
|
CMD [ "node", "./build/headplane/server.js" ]
|
||||||
|
|||||||
21
server.mjs
21
server.mjs
@ -55,13 +55,32 @@ const handler = remixRequestHandler(build, 'production')
|
|||||||
const http = createServer(async (req, res) => {
|
const http = createServer(async (req, res) => {
|
||||||
const url = new URL(`http://${req.headers.host}${req.url}`)
|
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
|
// 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
|
// if we can handle a raw file request. This is important for the
|
||||||
// Remix loader to work correctly.
|
// Remix loader to work correctly.
|
||||||
//
|
//
|
||||||
// To optimize this, we send them as readable streams in the node
|
// To optimize this, we send them as readable streams in the node
|
||||||
// response and we also set headers for aggressive caching.
|
// 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 filePath = join(baseDir, url.pathname.replace(PREFIX, ''))
|
||||||
const exists = existsSync(filePath)
|
const exists = existsSync(filePath)
|
||||||
const stats = statSync(filePath)
|
const stats = statSync(filePath)
|
||||||
|
|||||||
@ -6,9 +6,9 @@ import tsconfigPaths from 'vite-tsconfig-paths'
|
|||||||
|
|
||||||
installGlobals()
|
installGlobals()
|
||||||
|
|
||||||
const prefix = process.env.__INTERNAL_PREFIX || '/admin/'
|
const prefix = process.env.__INTERNAL_PREFIX || '/admin'
|
||||||
if (!prefix.endsWith('/')) {
|
if (prefix.endsWith('/')) {
|
||||||
throw new Error('Prefix must end with a slash')
|
throw new Error('Prefix must not end with a slash')
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineConfig(({ isSsrBuild }) => {
|
export default defineConfig(({ isSsrBuild }) => {
|
||||||
@ -43,11 +43,11 @@ export default defineConfig(({ isSsrBuild }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return ({
|
return ({
|
||||||
base: prefix,
|
base: `${prefix}/`,
|
||||||
build: isSsrBuild ? { target: 'ES2022' } : {},
|
build: isSsrBuild ? { target: 'ES2022' } : {},
|
||||||
plugins: [
|
plugins: [
|
||||||
remix({
|
remix({
|
||||||
basename: prefix,
|
basename: `${prefix}/`,
|
||||||
}),
|
}),
|
||||||
tsconfigPaths(),
|
tsconfigPaths(),
|
||||||
babel({
|
babel({
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user