Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Migrate away from document.write? #1910

Closed
jaidetree opened this issue Oct 14, 2021 · 2 comments
Closed

Migrate away from document.write? #1910

jaidetree opened this issue Oct 14, 2021 · 2 comments

Comments

@jaidetree
Copy link

Issue details

image

Chrome and Brave will display a warning in the console:

resize:38 [Violation] Avoid using document.write(). https://developers.google.com/web/updates/2016/08/removing-document-write

Steps to reproduce/test case

  1. Setup browser-sync to either behave as a proxy or a static server
  2. Run browser-sync
  3. Visit the browser-sync webserver
  4. Open the inspector in either Chrome or Brave

Please specify which version of Browsersync, node and npm you're running

  • Browsersync [ 2.27.5]
  • Node [16.10.0]
  • Npm [7.24.0]

It seems to me like using document.write is considered a bad-practice. Based on #1849 (comment) it was argued that it doesn't matter given its only running in development.

It's understandable that it's a non-issue given it's only used in development, but it seems to me like it would be worth fixing to avoid the warning. Subjectively, I feel it sets a better example not using APIs like document.write for devs just getting started and future web devs.

The warning does link to https://developers.google.com/web/updates/2016/08/removing-document-write but that really mostly affects production sites, which is a non-issue here.

Lastly, I would be willing to write a PR to fix it if a maintainer is onboard with changing it. My instinct would be to use document.createElement and document.body.appendChild APIs instead, open to recommendations if there's a better API. Would you be up for a PR to fix this?

@ouweiya
Copy link

ouweiya commented Oct 20, 2021

Modify the injection code

snippetOptions: {
  rule: {
    match: /<head>/i,
    fn(snippet, match) {
      const {
        groups: { src },
      } = /src='(?<src>[^']+)'/u.exec(snippet);
      return `<script src="${src}" async></script>${match}`;
    },
  },
}

gulp complete code

const createServe = cd => {
  browserSync.init({
    server: './build',
    notify: false,
    ui: false,
    open: false,
    snippetOptions: {
      rule: {
        match: /<head>/i,
        fn(snippet, match) {
          const {
            groups: { src },
          } = /src='(?<src>[^']+)'/u.exec(snippet);
          return `<script src="${src}" async></script>${match}`;
        },
      },
    },
  });
  cd();
};

Code reference
#1600 (comment)

@shakyShane
Copy link
Contributor

fixed in browser-sync@2.28.0

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants