Azure / static-web-apps

Azure Static Web Apps. For bugs and feature requests, please create an issue in this repo. For community discussions, latest updates, kindly refer to the Discussions Tab. To know what's new in Static Web Apps, visit https://aka.ms/swa/ThisMonth
https://aka.ms/swa
MIT License
326 stars 56 forks source link

Redirect based on header #282

Open ahmet8282 opened 3 years ago

ahmet8282 commented 3 years ago

Is it possible to make a redirect based on Accepted-Language header?

anthonychu commented 3 years ago

@ahmettahasakar can you share the scenario here? This is not possible today without using Azure Functions to process every request.

ahmet8282 commented 3 years ago

When a user accesses

http://example.com

the redirect route checks for Accepted-Language header. Based on the header redirects to

if fr then http://example.com/fr Or if de the http://example.com/de Or if doesnt exist or is en http://example.com/en

i am not sure if it is possible to do it via functions and then redirect to static pages

anthonychu commented 3 years ago

I think you can do something like this...

Create an API function, for example /api/selectlanguage. The function looks something like this:

module.exports = async function (context, req) {
    // select language based on req.headers
    context.res = {
        status: 302,
        headers: {
            "location": `/${lang}` // this would be something like "/fr"
        }
    };
};

And create a routing rule like this in routes.json:

{
  "routes": [
    {
      "route": "/",
      "serve": "/api/selectlanguage"
    }
  ]
}

This should do the redirect based on language when you visit the root of the site. I don't think you can do this on any page yet (like /* to /fr/*).

itpropro commented 3 years ago

I would recommend to just do it inside of the web application using the user's preferred languages and fallback to the header if required.

ahmet8282 commented 3 years ago

Hmm. Ok isnt server side redirect better for SEO?

itpropro commented 3 years ago

Yes you are right, 301 redirects and meta tags are sometimes important for SEO. I thought it was only about detecting the actual user language and serve a specific version of the page. Anyway, regarding to Google you should "Avoid automatic redirection based on the user's perceived language. These redirections could prevent users (and search engines) from viewing all the versions of your site." (https://developers.google.com/search/docs/advanced/crawling/managing-multi-regional-sites?visit_id=637492565632361557-3864116147&rd=1#let-the-user-switch-the-page-language)

sebastian-lenz commented 3 years ago

We do this kind of redirect for virtually all our sites here in Europe as we usually offer all sites in at least two different languages. The redirect happens solely on the root route "/" and forwards the user to his language, so the comment about avoiding redirects does not match here.

I've tried the approach using a Azure Function but as this redirect lives on the root of our application the 2 second startup time when the function is cold is completely not acceptable. The costs for a higher tier Azure function bundle that would ensure fast responses are astronomic.

I would like to point out that this requirement is so common, it is actually part of the routing system when using Netlify: https://docs.netlify.com/routing/redirects/redirect-options/#redirect-by-country-or-language