gatsby-plugin-no-javascript icon indicating copy to clipboard operation
gatsby-plugin-no-javascript copied to clipboard

javascript is not stripped when building with path prefix

Open leoschweizer opened this issue 4 years ago • 5 comments

The plugin works as expected as long as I run a vanilla gatsby build.

However, when using the path prefix option, and then running gatsby build --prefix-paths, the script elements will not be filtered from the resulting page.

instead, they are included with the correct path prefix, like this:

<link as="script" rel="preload" href="/foo/framework-26f52d65ffe097091d9a.js"/>

leoschweizer avatar May 12 '20 11:05 leoschweizer

Can confirm this is happening.

JoaaoVerona avatar Jun 09 '20 01:06 JoaaoVerona

Me too. I can't use the plugin now on a blog that lives in a subdirectory.

pmkemper avatar Jun 11 '20 10:06 pmkemper

I think that prefixing script.name with __PATH_PREFIX__ in

`/${script.name}` === headComponent.props.href &&

and in

return pageScripts.find((script): boolean => postBodyComponent.type === 'script' && `/${script.name}` === postBodyComponent.props.src) === undefined

should help. Please mind that I have not tested this thoroughly.

I also noticed that the "polyfill" script does not get removed, so the existing filter should be updated to cover this script too.

mcagalj avatar Aug 17 '20 08:08 mcagalj

I also ran into having my polyfill spared.

joaomilho avatar Jan 27 '21 22:01 joaomilho

I think that prefixing script.name with __PATH_PREFIX__ in

`/${script.name}` === headComponent.props.href &&

and in

return pageScripts.find((script): boolean => postBodyComponent.type === 'script' && `/${script.name}` === postBodyComponent.props.src) === undefined

should help. Please mind that I have not tested this thoroughly.

I also noticed that the "polyfill" script does not get removed, so the existing filter should be updated to cover this script too.

Thanks a lot, @mcagalj! Your tip was very helpful.

I did my own fix on the outside of the npm package, as I am a little newbie with package management.

If anyone needs the correction urgently, here it is. Just create the gatsby-ssr.js file at the root of the project

https://gist.github.com/joaocarloscabral/1173496642a15772d13815126ee1dc73

joaocarloscabral avatar Mar 29 '21 13:03 joaocarloscabral