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

injectRoute request url is missing search params #9737

Closed
1 task
mattiaz9 opened this issue Jan 19, 2024 · 8 comments
Closed
1 task

injectRoute request url is missing search params #9737

mattiaz9 opened this issue Jan 19, 2024 · 8 comments
Labels
needs triage Issue needs to be triaged

Comments

@mattiaz9
Copy link

Astro Info

Astro                    v4.2.1
Node                     v20.10.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             none

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

No response

Describe the Bug

Example:

index.ts

export default function filesIntegration(): AstroIntegration {
  return {
    name: "files-integration",
    hooks: {
      "astro:config:setup": ({ injectRoute, command }) => {
        if (command === "dev") {
          injectRoute({
            pattern: "/_file",
            entrypoint: "./files-integration/endpoint.ts",
            prerender: false,
          });
        }
      },
    },
  };
}

endpoint.ts

export const GET: APIRoute = async ({ request, url }) => {
  return new Response(url.href, {
    status: 200,
    headers: {
      "Content-Type": `text/plain`,
      "Cache-Control": "public, max-age=31536000",
    },
  });
};

In this GET request the url.searchParams is empty even though the url has some.

What's the expected result?

url.searchParams should be filled with the url params

Link to Minimal Reproducible Example

https://codesandbox.io/p/devbox/astro-injected-route-jvptjh

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 Jan 19, 2024
@ematipico
Copy link
Member

Is it the same for const url = new URL(request.url)?

@lilnasy
Copy link
Contributor

lilnasy commented Jan 19, 2024

This is expected in static output.

@lilnasy
Copy link
Contributor

lilnasy commented Jan 19, 2024

It works if you set output to server, the dev server is acting according the configuration. Feel free to reopen if you think there is still an issue.

@lilnasy lilnasy closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2024
@mattiaz9
Copy link
Author

thank you @lilnasy for the clarification.
Maybe I'm missing something. I'm trying to "reverse engineering" the astro:assets integration but for files.

Looking at this: https://github.com/withastro/astro/blob/53c69dcc82cdf4000aff13a6c11fffe19096cf45/packages/astro/src/assets/endpoint/node.ts seems like the request has the search params. How is this working without having to change the output?

@lilnasy
Copy link
Contributor

lilnasy commented Jan 19, 2024

The file you linked only gets executed on node-based adapters in production server builds.

@lilnasy
Copy link
Contributor

lilnasy commented Jan 19, 2024

What I said earlier was incorrect. The node endpoint is used both by the node adapter and the dev server which special cases /_image.

// HACK! astro:assets uses query params for the injected route in `dev`
if (!buildingToSSR && pathname !== '/_image') {

@mattiaz9
Copy link
Author

@lilnasy So basically there is no way to make it work right? I can't use search params and neither path params because getStaticPaths() is required.

Is there any possibilities to change the codebase to be more flexible? like for instance using this check if (!buildingToSSR && !pathname.startsWith('/_'))

@lilnasy
Copy link
Contributor

lilnasy commented Jan 19, 2024

I'm guessing what you want is this:

#8847

Your issue prompted a discussion of possible options in our discord:
https://discord.com/channels/830184174198718474/845430950191038464/1197974743589077014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

3 participants