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

URL object response type for Netlify edge functions #2023

Closed
1 task
Youhan opened this issue Dec 24, 2023 · 3 comments
Closed
1 task

URL object response type for Netlify edge functions #2023

Youhan opened this issue Dec 24, 2023 · 3 comments
Labels
question Further information is requested

Comments

@Youhan
Copy link

Youhan commented Dec 24, 2023

Describe the feature

I was not sure if this goes to H3 or Nitro. Apologies if I am confused.

As per Netlify Edge functions documentation, the function can return a standard Response object or a URL object.
link: https://docs.netlify.com/edge-functions/api/#overview

The URL object is used for rewrites. For example, if you want to rewrite <username>.site.dev to www.side.dev/profile/<username>, they suggest using an edge function like this:

export default async (req : Request) => {
  const reqUrl = new URL(req.url)
  return new URL(`/profile/${reqUrl.hostname.split('.')[0]}/${reqUrl.pathname}`, 'https://www.site.dev/')
}

This function does not return a Response object, instead it returns a URL object. How that would be possible with Nitro/H3?

Here are links to Netlify example and docs:

Additional information

  • Would you be willing to help implement this feature?
@serhalp
Copy link
Contributor

serhalp commented Nov 12, 2024

@Youhan What are you trying to accomplish here? Nitro on Netlify will automatically turn configured redirects and rewrites into a configuration that Netlify understands, so they should work. Are you trying to return redirects/rewrites dynamically that can only be determined dynamically at runtime, hence needing to return a URL?

@Youhan
Copy link
Author

Youhan commented Nov 12, 2024

Yes, that's correct. I am trying to rewrite the www.side.dev/profile/<username> to <username>.site.dev dynamically.

@pi0
Copy link
Member

pi0 commented Nov 13, 2024

In an event handler you can use getRequestURL / sendRedirect

export default eventHandler(async (event) => {
  const reqUrl = getRequestURL(event);
  const newUrl = new URL(
    `/profile/${reqUrl.hostname.split(".")[0]}/${reqUrl.pathname}`,
    "https://www.site.dev/"
  );
  return sendRedirect(event, newUrl.href);
});

In h3 v2 / nitro v3:

  • You can directly use event.url instead of getRequestURL(event)
  • I made a feature request to investigate possibility of supporting return <URL> as redirect you can follow from here: Support URL as response unjs/h3#913

@pi0 pi0 added question Further information is requested and removed pending triage labels Nov 13, 2024
@pi0 pi0 closed this as completed Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants