denoland / fresh

The next-gen web framework.
https://fresh.deno.dev
MIT License
12.52k stars 645 forks source link

Subdomain routing #603

Open cup-dev opened 2 years ago

cup-dev commented 2 years ago

Right now it only seems possible to configure routes via path matching after the domain.

It would be nice to instead be able to conditionally route requests from different subdomains within fresh without requiring hacks.

e.g. the starter project supports api.mywebsite.com/joke instead of just mywebsite.com/api/joke

erictherobot commented 2 years ago

In terms of simplicity, I don't think this would be possible without some extra effort. To be honest, I haven't seen this pattern in many frameworks. Traditionally, subdomains are treated as separate apps.

You could maybe use middleware and do something like grab the pathname, listen for /api/joke and redirect to your subdomain destination. I use this technique for migrating an old blog to a new routing structure... but unfortunately not on a subdomain.

 const pathname = new URL(req.url).pathname;

  if (pathname === "/api/joke") {
    return Response.redirect(
      `https://api.mywebsite.com/joke`, // <<--- this will redirect anywhere you want it to go. could be any destination. 
      301
    );
  }

Could you please describe your requirements a little more or share a repo?

TheYuriG commented 1 year ago

sorry to bump this, but i would love if this was implemented. I want to be able to use the blog. subdomain to make my blog posts and shop. to host my fake shipping project. I'll use the solution above in the meantime.

iuioiua commented 1 year ago

I'm -1 on this. Having the same Fresh server listening across different subdomains sounds convoluted and rarely, if ever, a setup requirement.

TheYuriG commented 1 year ago

If you are against this request, please post a direction on how to have a different solution.

Should we host multiple projects, one for each subdomain, and then unify all of them through a hosting provider? Netlify offers this, I would assume that Deno Deploy does as well.

I just think that's it's kind of a waste to need to duplicate files and styling and brand icons and favicons over different projects (or setup and use a subrepository to keep them all in sync) when having it built in would be far easier for us, users, to manage.

I do, however, understand that the maintainers might just have bigger fish to fry, considering this issue has been stale over the past 9 months. I'm fine with this going both either direction, just would like to ask a definitive answer in either way because either answer would define how I'll evolve my project.

iuioiua commented 1 year ago

If you'd like to use the same files across your website, the simplest solution would be just to use a single host and separate your website by path. E.g. /blog and /shop 🙂

TheYuriG commented 1 year ago

E.g. /blog and /shop 🙂

so essentially not using subdomain routing like this issue is asking for

iuioiua commented 1 year ago

Correct. To be clear, I'm not even sure how this can be done. Are you able to find another web framework that does this?

TheYuriG commented 1 year ago

To be clear, I'm not even sure how this can be done.

I would assume you would append subdomains to __app.tsx dynamically based on what the subdomain is. I don't know what the /routes folder structure would look like for this. Maybe [_sub.(subdomainGoesHere)] and then inside you can have the index.tsx with everything else?

Are you able to find another web framework that does this?

Nuxt (where I come from) enables it: https://ynechaev.medium.com/subdomains-with-nuxt-js-483df826c788

Next seems to easily support it as well: https://vercel.com/guides/nextjs-multi-tenant-application

You can also hack your way through Remix to achieve this using Express routing: https://github.com/remix-run/remix/discussions/2886

OzairP commented 1 year ago

Correct. To be clear, I'm not even sure how this can be done. Are you able to find another web framework that does this?

Not Javascript, but a web framework: https://laravel.com/docs/10.x/routing#route-group-subdomain-routing