Skip to content

Commit

Permalink
fix(api-server): copy fallback fix from #9272 (#9369)
Browse files Browse the repository at this point in the history
This PR copies the fix in #9272
into `@redwoodjs/api-server`. The `@redwoodjs/api-server` can be
accessed via `npx @redwooodjs/api-server rw-server api`. We won't have
to do this once the code between the two entry points is deduplicated
but till then, yeah.
  • Loading branch information
jtoar committed Nov 3, 2023
1 parent 4bb8b33 commit 616b4d6
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/api-server/src/plugins/withWebServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs'
import path from 'path'

import fastifyStatic from '@fastify/static'
import type { FastifyInstance, FastifyReply } from 'fastify'
import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'

import { getPaths } from '@redwoodjs/project-config'

Expand Down Expand Up @@ -54,10 +54,21 @@ const withWebServer = async (

// For SPA routing fallback on unmatched routes
// And let JS routing take over
fastify.setNotFoundHandler({}, function (_, reply: FastifyReply) {
reply.header('Content-Type', 'text/html; charset=UTF-8')
reply.sendFile(indexPath)
})
fastify.setNotFoundHandler(
{},
function (req: FastifyRequest, reply: FastifyReply) {
const requestedExtension = path.extname(req.url)
// If it's requesting some sort of asset, e.g. .js or .jpg files
// Html files should fallback to the index.html
if (requestedExtension !== '' && requestedExtension !== '.html') {
reply.code(404)
return reply.send('Not Found')
}

reply.header('Content-Type', 'text/html; charset=UTF-8')
return reply.sendFile(indexPath)
}
)

return fastify
}
Expand Down

0 comments on commit 616b4d6

Please sign in to comment.