gatsby-plugin-s3 icon indicating copy to clipboard operation
gatsby-plugin-s3 copied to clipboard

Support prefix-based redirects.

Open microbit-matt-hillsdon opened this issue 5 years ago • 5 comments

Gatsby doesn't provide a way to configure this, but I think it might be reasonable to add plugin-specific redirect metadata (there's a cryptic "rest" field documented in the API docs for createRedirect).

Using a simple boolean seems most straightforward given AWS's limitations.

Is this crazy? Feedback much appreciated. Happy to look more closely at testing etc. if this is considered a reasonable approach.

microbit-matt-hillsdon avatar Mar 23 '20 17:03 microbit-matt-hillsdon

Do you have a use case for this? Would that use case be covered by #62, or perhaps by a config value that allows for defining custom rewrite rules?

I'm not ecstatic about extending GatsbyRedirect with something so AWS specific. Tell us a use case and then we'll see what Jari thinks.

YoshiWalsh avatar Mar 24 '20 06:03 YoshiWalsh

@JoshuaWalsh, thanks for taking a look.

In my case, I have a single Gatsby site deployed to an AWS bucket.

All redirects are managed in a headless CMS, then we call createRedirect in Gatsby, then gatsby-plugin-s3 deploys them.

We have a business requirement to redirect large site areas based on a prefix, so (in Netlify syntax) from /old/* to /new/:splat. We also want it to work for external redirects (in some cases we redirect old content to a separate archive site on a different domain).

I'd like to keep all redirects in the CMS, so they are visible to our CMS users. I'd also much prefer gatsby-plugin-s3 to completely own the S3 bucket rather than maintain redirect settings separately on the bucket and merge. Ideally, the information flow for prefixed redirects would be as similar as possible to regular redirects.

I think redirects are a somewhat plugin-specific (and messy!) area of Gatsby. For example, statusCode and force are documented in Gatsby and the Netlify plugin documented in the Netlify plugin as "Plugin-specific".

The Netlify plugin docs also show a Language field being used, which I think they use to redirect based on the header of the same name (not really explained properly in their plugin). If I was using Netlify, I think the redirects I need could be achieved using the :splat syntax via createRedirect. They wouldn't work in the browser though (Gatsby uses a simple from->to lookup there).

This discussion is interesting: https://github.com/gatsbyjs/gatsby/issues/11056. I can see how a difference between client-side and server-side redirect capability could cause issues. For me, because of how the CMS is used, I know there will never be on-site links to old URLs, so server-side only handling of those is fine.

One alternative to the prefix field would be to support only a trailing wildcard and :splat, and maybe a plugin config flag to turn on this processing. I felt that a separate prefix?: boolean field was a bit more honest about AWS's restricted capabilities here, but compatibility with a subset of the Netlify syntax might have value.

Another might be the ability to provide the plugin with AWS format redirects (perhaps via a file) and have it merge the lists before applying the config to the bucket. This would be a bit of a shame, as my code would have to know about the details of the messier AWS API to generate them.

Thanks again for engaging with this.

microbit-matt-hillsdon avatar Mar 25 '20 10:03 microbit-matt-hillsdon

I agree that redirects are often plugin-specific and messy. I was hoping to avoid contributing to that mess, but your use case seems solid to me.

Looking at your code it seems like a clean way to do it. I can see you believe strongly in DRY. The only real complaint I have is the name maybeNonBucketLevelRedirects, but I'm struggling to think of a better name. possibleObjectBasedRedirects? objectEligibleRedirects?

If I'm nitpicking, your use of identity is perhaps a bit too clever for my liking, I don't really like that trailingSlashTransform will sometimes be a no-op. I'm wondering if there's a more clear way to achieve the same result. (But I'm glad you included this way, because until now I wasn't familiar with the concept of identity functions and now I know that JoshuaWalsh/ts-demand totally reinvents the wheel.)

@jariz What are your thoughts on this? Do you think this feature is justified?

YoshiWalsh avatar Mar 25 '20 11:03 YoshiWalsh

Hi @microbit-matt-hillsdon, we now have a bucketPrefix option, does this fix your problems or...? I'm still kind of struggling to understand this feature on first glance.

jariz avatar Jul 23 '20 00:07 jariz

I was looking for a way to generate server redirections that "rewrite" the URLs like /old/* -> /new/*. I tried this PR, and it seems that it fit my needs.

@jariz I'm starting to use Gatsby so I certainly miss something, but how bucketPrefix could help me with redirections?

ysavary avatar Jul 28 '21 17:07 ysavary