netlify / netlify-plugin-gatsby

A build plugin to integrate Gatsby seamlessly with Netlify

Home Page:https://www.npmjs.com/package/@netlify/plugin-gatsby

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

404 pages don't work when a site has Functions but no SSR/DSG pages

moneal opened this issue · comments

Deploying a Gatsby 4 site with functions defined in the api folder breaks 404 handling in production.

If trying to access a non-existent page like /404-test triggers the below error:

{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module '/var/task/.cache/page-ssr'\nRequire stack:\n- /var/task/.netlify/functions-internal/__dsg/__dsg.js\n- /var/task/__dsg.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","trace":["Runtime.ImportModuleError: Error: Cannot find module '/var/task/.cache/page-ssr'","Require stack:","- /var/task/.netlify/functions-internal/__dsg/__dsg.js","- /var/task/__dsg.js","- /var/runtime/UserFunction.js","- /var/runtime/index.js","    at _loadUserApp (/var/runtime/UserFunction.js:202:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:242:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:1085:14)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)","    at Module.load (internal/modules/cjs/loader.js:950:32)","    at Function.Module._load (internal/modules/cjs/loader.js:790:12)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)","    at internal/main/run_main_module.js:17:47"]}

If you rename the gatsby api folder to _api and redeploy 404 pages start working correctly.

gatsby-plugin-netlify version 4.1 is installed and enabled.

Hi @moneal
Could you share a reproduction? I've tried a demo site which has api functions and it works fine for me: https://deploy-preview-329--netlify-plugin-gatsby-demo.netlify.app/404test

Never mind: I've reproduced it! It happens when there are functions but no SSR or DSG pages. I'll rename to reflect this.

The error is because the site adds a catch-all redirect for DSG pages, but there is no SSR bundle. This can be fixed if we add specific redirects for DSG pages as well as SSR, rather than relying on a catch-all.

Hey @moneal. We will have a fix available for this shortly.