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

page-data is still preloaded #8

Closed marvinrabe closed 4 years ago

marvinrabe commented 5 years ago

HTML still contains:

<link as="fetch" rel="preload" href="/page-data/index/page-data.json" crossorigin="use-credentials"/>

This has no purpose without JavaScript but still gets downloaded.

itmayziii commented 5 years ago

@marvinrabe

Thank you for the issue @marvinrabe, could you please share your gatsby-config.js file so I can better understand how this would happen?

itmayziii commented 5 years ago

Going to leave this issue open a bit longer but I have not been able to reproduce this myself. Unless @marvinrabe can provide a reproduction of the issue soon then I will have to close this.

marvinrabe commented 5 years ago

This happens when using a gatsby source plugin for fetching remote data.

This can be seen with: https://github.com/TryGhost/gatsby-starter-ghost

mathiasha commented 4 years ago

This is also an issue for me.

What i originally did (before I found this plugin) was i created a gatsby-ssr.js like this:

const get = require('lodash/get');

function filterJavascriptItems(item) {
  const href = get(item, 'props.href');
  const src = get(item, 'props.src');
  const expression = new RegExp('.(js|json)$');

  if (href && expression.exec(href)) {
    return false;
  }

  if (src && expression.exec(src)) {
    return false;
  }

  return true;
}

exports.onPreRenderHTML = ({
  getHeadComponents,
  replaceHeadComponents,
  getPostBodyComponents,
  replacePostBodyComponents
}) => {
  if (process.env.NODE_ENV === 'production') {
    const filteredHeadComponents = getHeadComponents().filter(
      filterJavascriptItems
    );
    replaceHeadComponents(filteredHeadComponents);
    const filteredPostBodyComponents = getPostBodyComponents().filter(
      filterJavascriptItems
    );
    replacePostBodyComponents(filteredPostBodyComponents);
  }
};

This approach is based on @jordanoverbye https://github.com/jordanoverbye/gatsby-plugin-no-js

itmayziii commented 4 years ago

@marvinrabe @mathiasha

This is now fixed with v2.0.5