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

Localised error pages cause redirect loops #249

Open
sagge opened this issue May 6, 2021 · 4 comments
Open

Localised error pages cause redirect loops #249

sagge opened this issue May 6, 2021 · 4 comments

Comments

@sagge
Copy link

sagge commented May 6, 2021

Hi,

I am facing some challenges as I'm trying to implement localised error pages for our website. For some reason I am ending up with redirect loops in different locations depending on the configuration. The page is structured so that "/" path is for default locale and other locales are prefixed with langKey, for example "/sv/". I am creating the localised error pages as follows:

exports.onCreatePage = async ({ page, actions }) => {
  const { createPage, deletePage } = actions
  // Check if the page is a localized 404
  if (page.path.match(/^\/[a-z]{2}\/404\/$/)) {
    const oldPage = { ...page }
    const langCode = page.context.langKey;
    if (langCode !== 'fi') {
      page.matchPath = `/${langCode}/*`
    }
    // Recreate the modified page
    deletePage(oldPage)
    createPage(page)
  }
}

The error page for default language is configured to the default 404.html.

If I use this for page creation and have the S3 plugin configured as follows:

{
  resolve: `gatsby-plugin-s3`,
  options: {
    bucketName: 'our-website-bucket',
    protocol: siteAddress.protocol.slice(0, -1),
    hostname: siteAddress.hostname,
    generateRedirectObjectsForPermanentRedirects: true,
    params: {
      'sitemap.xml': {
        CacheControl: 'public, max-age=0, must-revalidate',
      },
    },
  },
}

Everything seems to be working fine in the main locale under "/" path (also the 404 page). Other locales (for example "/sv/") are also fine when navigating to pages that exist, but anything that can't be found, for example "/sv/foobar" causes ERR_TOO_MANY_REDIRECTS.

If I change the configuration to include generateMatchPathRewrites: false, the localised error pages seem to be working fine, except when doing a hard refresh (cmd + shift + r) on the index page for particular locale (for example in path "/sv/"). Hard refresh navigates to "/sv/404" and causes ERR_TOO_MANY_REDIRECTS. Regular refresh on the index path for different locales are working fine.

Is there something I could configure differently to prevent the redirect loops from happening? Tips and help would be greatly appreciated as I am hitting a wall myself.

@YoshiWalsh
Copy link
Collaborator

YoshiWalsh commented May 10, 2021

This is a strange issue, and I'm not that familiar with the matchPath property. (Which I think might be what's causing the redirect to be created)

Could you please try disabling generateRedirectObjectsForPermanentRedirects and sharing what redirect rules end up in your S3 Static Website config?

@sagge
Copy link
Author

sagge commented May 19, 2021

Hi,

Sorry for the delay on getting back to you. I tried building the website with different combinations of generateRedirectObjectsForPermanentRedirects and generateMatchPathRewrites options. Different combinations generate the following configs:

Combination 1:

generateRedirectObjectsForPermanentRedirects: true,
generateMatchPathRewrites: true,

s3.routingRules.json:

[{"Condition":{"KeyPrefixEquals":"sv/"},"Redirect":{"ReplaceKeyWith":"sv/404","HttpRedirectCode":"302","Protocol":"https","HostName":"ourdomain.com"}}]

Combination 2:

generateRedirectObjectsForPermanentRedirects: true,
generateMatchPathRewrites: false,

s3.routingRules.json:

[]

Combination 3:

generateRedirectObjectsForPermanentRedirects: false,
generateMatchPathRewrites: true,

s3.routingRules.json:

[{"Condition":{"KeyPrefixEquals":"sv/"},"Redirect":{"ReplaceKeyWith":"sv/404","HttpRedirectCode":"302","Protocol":"https","HostName":"ourdomain.com"}},{"Condition":{"KeyPrefixEquals":"artikkelit/perunkirjoitus/perunkirjoitus-ja-virkatodistukset/"},"Redirect":{"ReplaceKeyWith":"artikkelit/perunkirjoitus/virkatodistus-perunkirjoitusta-varten","HttpRedirectCode":"301","Protocol":"https","HostName":"ourdomain.com"}},{"Condition":{"KeyPrefixEquals":"artikkelit/perunkirjoitus/virkatodistus/"},"Redirect":{"ReplaceKeyWith":"artikkelit/perunkirjoitus/virkatodistus-perunkirjoitusta-varten","HttpRedirectCode":"301","Protocol":"https","HostName":"ourdomain.com"}},{"Condition":{"KeyPrefixEquals":"artikkelit/avioliitto/sahkoinen-avioerohakemus/"},"Redirect":{"ReplaceKeyWith":"artikkelit/avioliitto/avioerohakemus","HttpRedirectCode":"301","Protocol":"https","HostName":"ourdomain.com"}}]

Combination 4:

generateRedirectObjectsForPermanentRedirects: false,
generateMatchPathRewrites: false,

s3.routingRules.json:

[{"Condition":{"KeyPrefixEquals":"artikkelit/perunkirjoitus/perunkirjoitus-ja-virkatodistukset/"},"Redirect":{"ReplaceKeyWith":"artikkelit/perunkirjoitus/virkatodistus-perunkirjoitusta-varten","HttpRedirectCode":"301","Protocol":"https","HostName":"ourdomain.com"}},{"Condition":{"KeyPrefixEquals":"artikkelit/perunkirjoitus/virkatodistus/"},"Redirect":{"ReplaceKeyWith":"artikkelit/perunkirjoitus/virkatodistus-perunkirjoitusta-varten","HttpRedirectCode":"301","Protocol":"https","HostName":"ourdomain.com"}},{"Condition":{"KeyPrefixEquals":"artikkelit/avioliitto/sahkoinen-avioerohakemus/"},"Redirect":{"ReplaceKeyWith":"artikkelit/avioliitto/avioerohakemus","HttpRedirectCode":"301","Protocol":"https","HostName":"ourdomain.com"}}]

Hopefully this will shed some light on the issue.

@Tahriff
Copy link

Tahriff commented Nov 12, 2021

Did you manage to solve your issue ? I'm encountering a very similar situation.

@sagge
Copy link
Author

sagge commented Nov 12, 2021

Unfortunately no. I couldn't find a way to get this working and ended up using single error page with the default language for the whole site. This is quite far from optimal as it only works for certain language and causes the site language to change to default.

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

No branches or pull requests

3 participants