Redirect not working for external JS file

Hi there,

Let’s say I have the following in my netlify.toml file:

[[redirects]]
  from = "/external-service/*/link.js"
  to = "https://api.external-service.io/v1/:splat/analytics.min.js"
  status = 200
  force = true

The * can be anything (we don’t need to know what it is explicitly).

However when doing this the redirect doesn’t work. Instead the netlify site actually tries to find this explicit file and naturally can’t find it. So for example doing the following tries to resolve

<script async src="/external-service/some-randomly-generated-string/link.js"></script>

We have this in the _redirects file in the public folder if this helps

# SPA
/*    /index.html   200

I’ve read the docs but none explicitly mention this scenario when I want to proxy a JS file. Any help would be appreciated

@atomicshapes Welcome to the Netlify community.

The * wildcard and splats work only at the ends of URLs. It won’t work otherwise.

Hey thanks that super helpful, so what’s the recommended way to achieve what I need here? :slight_smile:

Without knowing your set-up, I can only say that you have to have the explicit path to the JS file for the redirect. If you have only one or two, this should be trivial. If you have different link.js files scattered around your site, then you’ll have to refactor to get around this.

Yeah so the random string that is between the URL is served from a 3rd party so will never be consistent which is a little awkward, essentially I just need the URL to be able to appear as if it’s being served from the domain being visited and remove the word analytics

just to update @gregraven i have added the following to my _redirect
/segments/file.js https://cdn.external-service.com/analytics.js/v1/my-super-secret-write-key/analytics.min.js 200

I replace my-super-secret-write-key with sed at build time, however it appears the file is still not being found when I do
<script async src="/segments/file.js"></script>

Is there any particular reason this isn’t working?

Hi, @atomicshapes, I think the issue is the use of the wildcard instead of the token:

[[redirects]]
  from = "/external-service/*/link.js"
  to = "https://api.external-service.io/v1/:splat/analytics.min.js"
  status = 200
  force = true

It might help to change this to:

[[redirects]]
  from = "/external-service/:token/link.js"
  to = "https://api.external-service.io/v1/:token/analytics.min.js"
  status = 200
  force = true

So changing “*” and “:splat” both to “:token”.

If that doesn’t fix it, please let us know.

@luke Very cool. Where is this documented?

Hey greg, you can see documentation on this behaviour here:

https://docs.netlify.com/routing/redirects/redirect-options/#placeholders

The word “token” sounds a bit like it would do something magical, but is actually not a special word. The placeholder could be “toaster” as long as it matches :+1:

@perry Thanks. I NEVER would have gotten this usage from this documentation.

1 Like

our luke is a snappy guy :smiley: