itmayziii / gatsby-plugin-no-javascript

Removes all javascript files created by Gatsby from the static HTML files.
MIT License
77 stars 13 forks source link

javascript is not stripped when building with path prefix #17

Open leoschweizer opened 4 years ago

leoschweizer commented 4 years ago

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"/>
JoaaoVerona commented 4 years ago

Can confirm this is happening.

pmkemper commented 4 years ago

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

mcagalj commented 4 years ago

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.

joaomilho commented 3 years ago

I also ran into having my polyfill spared.

joaocarloscabral commented 3 years ago

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