Repository navigation
Use static 404.html with server api-routes on Netlify #3241
codeflorist
started this conversation in
Help Wanted
Replies: 2 comments 1 reply
|
I have found a workaround for this. After building, the actual function-config for netlify resides in: Here the Here is a script, that can be run after build to make the replacement in the file: import fs from 'node:fs'
const netlifyConfigFile = './.netlify/functions-internal/server/server.mjs'
if (!fs.existsSync(netlifyConfigFile)) {
console.log(`Error: File "${netlifyConfigFile}" does not exist.`)
process.exit(1)
}
let netlifyConfigFileContents = fs.readFileSync(netlifyConfigFile, 'utf8')
netlifyConfigFileContents = netlifyConfigFileContents.replace(
'path: "/*"',
'path: "/api/*"'
)
fs.writeFileSync(netlifyConfigFile, netlifyConfigFileContents, (error) => {
if (error) {
throw error
}
}) |
0 replies
|
Hello ! This looks perfect for me, how do I run that script after the build? Do i save it to my repo and somehow launch it in my netlify.toml? |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
When deploying a Nuxt 3 application, that has prerendered as well as server-side api-routes to Netlify (using the
netlifypreset), it seems it's not possible to use a static404.htmlat the moment.So basically i'd like this redirect logic:
Nitro creates the
netlify.tomlfile with this config:I can also see this in the Netlify backend:

I guess this means, that ALL requests, that are not resolvable to a static file, are hitting the function. This is perfectly fine (and needed) in a Nuxt app, that uses SSR for certain or all pages. But for an app, that is fully prerendered, any only uses a handful of api-routes, this means a LOT of unnecessary requests (bots, ddos, etc.) are hitting the function - causing unnecessary load on the function and higher bills.
I guess the solution would be to specifically only foward the existing api- and server-routes or "turn off" rendering of SSR completely, so Netlify falls back to the static 404.html.
Is this possible at the moment already somehow? If not, i think that would be a very useful feature.
All reactions