Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vercel adapter: parameter "functionPerRoute" generate serverless function even for prerendered routes #10211

Closed
1 task
bentouch-digital opened this issue Feb 23, 2024 · 5 comments · Fixed by #10231
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) pkg: astro Related to the core `astro` package (scope)

Comments

@bentouch-digital
Copy link

Astro Info

Astro                    v4.4.2
Node                     v20.10.0
System                   macOS (arm64)
Package Manager          yarn
Output                   hybrid
Adapter                  @astrojs/vercel/serverless
Integrations             @astrojs/tailwind
                         astro-icon
                         @astrojs/vue
                         @storyblok/astro
                         extract-tailwind-classes
                         redirections

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

All

Describe the Bug

Actually, when I build the projet with the Vercel adapter and the parameter functionPerRoute to true, it generates serverless functions for all routes, prerendered ones too. I have a route named [...slug].astro that generates only prerendered pages. But with this configuration it generates a serverless function [...slug].astro, as the result, all 404 errors fallback on this serverless function and a response is returned with code 200. If i set functionPerRouteto false, it's works as intended.

What's the expected result?

Must not generate serverless functions for prerendered pages.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-9beo4b

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Feb 23, 2024
@ematipico ematipico added pkg: astro Related to the core `astro` package (scope) - P3: minor bug An edge case that only affects very specific usage (priority) and removed needs triage Issue needs to be triaged labels Feb 23, 2024
@matthewp
Copy link
Contributor

Your example doesn't have prerendered pages. Please update the example to match the problem you're describing.

@matthewp matthewp added the needs repro Issue needs a reproduction label Feb 23, 2024
Copy link
Contributor

Hello @bentouch-digital. 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.

@dylanalizon
Copy link

Your example doesn't have prerendered pages. Please update the example to match the problem you're describing.

@matthewp Hello, the ouput is "hybrid", unless I'm mistaken all pages are prerendered by default, so the index.astro is a prerendered one.

@lilnasy lilnasy removed the needs repro Issue needs a reproduction label Feb 23, 2024
@lilnasy
Copy link
Contributor

lilnasy commented Feb 23, 2024

Good first issue!

This is an opportunity for astro users to learn about how astro works by fixing a small but real bug. You can expect to get help setting up the dev environment, and understanding the project structure in #dev channel of our discord server (https://astro.build/chat).

What's the bug?

The functionPerRoute config creates more functions than necessary.

What's going wrong here?

Adapters depend on Astro's Integration API to provide them with the routes to make functions out of. The routes are provided in a map called entrypoints.

Neither Astro nor the vercel adapter make a distinction between prerendered and on-demand rendered routes - all are turned into functions. Only non-prerendered routes should be exposed to the adapter.

Where do I start looking?

The part where Astro's internals pass the entrypoints map to the adapter is here:

await runHookBuildSsr({
config: options.settings.config,
manifest,
logger: options.logger,
entryPoints: internals.entryPoints,
middlewareEntryPoint: shouldPassMiddlewareEntryPoint
? internals.middlewareEntryPoint
: undefined,
});

The part where Astro's internals populate the entrypoints map in the first place is here:

for (const moduleKey of Object.keys(chunk.modules)) {
if (moduleKey.startsWith(RESOLVED_SPLIT_MODULE_ID)) {
internals.ssrSplitEntryChunks.set(moduleKey, chunk);
storeEntryPoint(moduleKey, options, internals, chunk.fileName);
}
}

@mingjunlu
Copy link
Contributor

@lilnasy Thanks for providing guidance on this issue! I would like to give it a try 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants