withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
46.98k stars 2.5k forks source link

output: "hybrid" fails to show 404 page. #9615

Closed disrae closed 10 months ago

disrae commented 10 months ago

Astro Info

Astro                    v4.0.6
Node                     v20.8.0
System                   macOS (arm64)
Package Manager          npm
Output                   hybrid
Adapter                  @astrojs/vercel/serverless
Integrations             @astrojs/tailwind
                         @astrojs/mdx
                         @astrojs/sitemap
                         @storyblok/astro

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When using hybrid rendering, if I push my .vercel/ directory to the repo the 404 page function does not work. 404 with: ✅ Static rendering ✅ Hybrid localhost with 404 as function or static ❌ Hybrid on Vercel with 404 as function or static returns 500 error

500 error:

2024-01-04T23:28:32.305Z    undefined   ERROR   Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/dist/entry.mjs' imported from /var/task/___vc/__launcher/bridge-server-72TT5FOD.js
    at new NodeError (node:internal/errors:406:5)
    at finalizeResolution (node:internal/modules/esm/resolve:238:11)
    at moduleResolve (node:internal/modules/esm/resolve:850:10)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:972:12)
    at defaultResolve (node:internal/modules/esm/resolve:1091:79)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:228:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:315:34)
    at importModuleDynamically (node:internal/modules/cjs/loader:1164:37) {
  url: 'file:///var/task/dist/entry.mjs',
  code: 'ERR_MODULE_NOT_FOUND'
}

### Documentation Followed
https://docs.astro.build/en/guides/server-side-rendering/#add-an-adapter
https://docs.astro.build/en/guides/server-side-rendering/#opting-out-of-pre-rendering-in-hybrid-mode

INIT_REPORT Init Duration: 187.44 ms    Phase: init Status: error   Error Type: Runtime.ExitError
2024-01-04T23:28:32.552Z    undefined   ERROR   Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/dist/entry.mjs' imported from /var/task/___vc/__launcher/bridge-server-72TT5FOD.js
    at new NodeError (node:internal/errors:406:5)
    at finalizeResolution (node:internal/modules/esm/resolve:238:11)
    at moduleResolve (node:internal/modules/esm/resolve:850:10)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:972:12)
    at defaultResolve (node:internal/modules/esm/resolve:1091:79)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:228:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:315:34)
    at importModuleDynamically (node:internal/modules/cjs/loader:1164:37) {
  url: 'file:///var/task/dist/entry.mjs',
  code: 'ERR_MODULE_NOT_FOUND'
}
INIT_REPORT Init Duration: 211.74 ms    Phase: invoke   Status: error   Error Type: Runtime.ExitError
Error: Runtime exited with error: exit status 1

What's the expected result?

Expected 404 serverless function to return the 404 page. https://docs.astro.build/en/guides/server-side-rendering/#add-an-adapter https://docs.astro.build/en/guides/server-side-rendering/#opting-out-of-pre-rendering-in-hybrid-mode

Link to Minimal Reproducible Example

https://goose-website-cbarwkiun-goose-insurance-1c431eed.vercel.app/en-us/should-go-to-404

Participation

github-actions[bot] commented 10 months ago

Hello @disrae. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

lilnasy commented 10 months ago

This seems like an important issue. However, we can't investigate without a simple project to debug and try fixes on.

disrae commented 10 months ago

Okay I will do, however the repro will have to be deployed on vercel for it to cause the issue.

lilnasy commented 10 months ago

@disrae could you try with npm install @astrojs/vercel@experimental--9624?

disrae commented 10 months ago

Hmm, it works in the new project, let me try upgrading astro from 4.0.6 to 4.1 or to your version. Reproduction repo site, which works, not much to see here

Could there be some kind of build cache on Vercel that I need to clear?

disrae commented 10 months ago

This seems like the reproduction is a production deploy? image

Also, the issue happens on vercel in preview deploys.

lilnasy commented 10 months ago

Yeah, I noticed from the URL - it isn't the same issue I ran into earlier.

Your old project (linked in the issue description) responds with a 500. What do the logs say?

disrae commented 10 months ago

one second... reproducing....

disrae commented 10 months ago

Okay, upon re-reproducing in the main project, the patch version for the vercel adapter incremented by 2, and the issue seems to be gone!

disrae commented 10 months ago

Latest Vercel adapter package seems to have solved the issue. "@astrojs/vercel": "^6.1.2"

disrae commented 10 months ago

Okay, it is not solved. The bug happens when I commit the files from the .vercel directory, without it it still works, and I'm getting the logs as well, which is the only reason I am using hybrid mode, to see which pages are missing redirects, aka 404ing. image

lilnasy commented 10 months ago

Is the result any different when using the vercel CLI?

pnpm exec astro build
pnpm exec vercel deploy --prebuilt --prod
disrae commented 10 months ago

Sorry bogged down, but trying to get to that.

disrae commented 10 months ago

vercel deploy gave me some trouble, said I didn't have a project by the name, despite being in the correct account and such, then it seemed to not automatically know it was an Astro project and the defaults it was presenting didn't seem right, so I backed out. I did however manage to reproduce the issue by pushing the .vercel/ directory, is that directory meant to be strictly local?

Reproduction: repo demo logs:

2024-01-06T01:16:20.477Z    undefined   ERROR   Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/dist/entry.mjs' imported from /var/task/___vc/__launcher/bridge-server-72TT5FOD.js
    at new NodeError (node:internal/errors:406:5)
    at finalizeResolution (node:internal/modules/esm/resolve:238:11)
    at moduleResolve (node:internal/modules/esm/resolve:850:10)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:972:12)
    at defaultResolve (node:internal/modules/esm/resolve:1091:79)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:228:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:315:34)
    at importModuleDynamically (node:internal/modules/cjs/loader:1164:37) {
  url: 'file:///var/task/dist/entry.mjs',
  code: 'ERR_MODULE_NOT_FOUND'
}
INIT_REPORT Init Duration: 191.03 ms    Phase: init Status: error   Error Type: Runtime.ExitError
2024-01-06T01:16:20.703Z    undefined   ERROR   Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/dist/entry.mjs' imported from /var/task/___vc/__launcher/bridge-server-72TT5FOD.js
    at new NodeError (node:internal/errors:406:5)
    at finalizeResolution (node:internal/modules/esm/resolve:238:11)
    at moduleResolve (node:internal/modules/esm/resolve:850:10)
    at moduleResolveWithNodePath (node:internal/modules/esm/resolve:972:12)
    at defaultResolve (node:internal/modules/esm/resolve:1091:79)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:228:38)
    at ModuleLoader.import (node:internal/modules/esm/loader:315:34)
    at importModuleDynamically (node:internal/modules/cjs/loader:1164:37) {
  url: 'file:///var/task/dist/entry.mjs',
  code: 'ERR_MODULE_NOT_FOUND'
}
INIT_REPORT Init Duration: 214.27 ms    Phase: invoke   Status: error   Error Type: Runtime.ExitError
Error: Runtime exited with error: exit status 1

image image

lilnasy commented 10 months ago

From the error message, I can tell that vercel's platform is looking at wrong place. No astro code has run before that error occurs.

It might be because install/build/start commands are set to a wrong preset in the project config. Can you try again by deploying to a new project?

lilnasy commented 10 months ago

Nothing wrong with your example itself. https://repro-9615.vercel.app

astro build image
vercel deploy image