Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

small fix in init_service_worker #5417

Merged

Conversation

NikolayMakhonin
Copy link
Contributor

Replaced arrow function with anonymous function in the init_service_worker script for support old browsers

This is necessary for the application to work in WebView on Android >= 5.1 (Chromium >= 39).
Arrow functions are only supported in Android WebView >= 45, see MDN

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. All changesets should be patch until SvelteKit 1.0

fix: Replaced arrow function with anonymous function in the init_service_worker script for support old browsers
@changeset-bot
Copy link

changeset-bot bot commented Jul 8, 2022

🦋 Changeset detected

Latest commit: 778c4c4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Copy link
Member

I'm curious how you're getting the rest of the app to work, since we're using <script type="module"> etc? I ask because this is the sort of thing that could easily regress unless we have some way of guarding against it, but I'm not sure what that looks like

@NikolayMakhonin
Copy link
Contributor Author

NikolayMakhonin commented Jul 8, 2022

I'm curious how you're getting the rest of the app to work, since we're using <script type="module"> etc? I ask because this is the sort of thing that could easily regress unless we have some way of guarding against it, but I'm not sure what that looks like

It looks like the <script type="module"> doesn't run in Chromium 39 at all, because there are no errors in the console.
I pasted my script at the end of the html which replaces all <script type="module"> with shimport script. I write the code from <script type="module"> to a blob URL, and pass this URL to the shimport script. It turns out a polyfill that does not require changes in the original code. But I haven't tested this in other browsers.

app.html

<body>
  <div>%sveltekit.body%</div>
  <script src="/lib/polyfills/shimport/polyfill.js"></script>
</body>

/lib/polyfills/shimport/polyfill.js

try {
  new Function("if(0)import('')")();
} catch (e) {
  var scripts = document.querySelectorAll('script[type=module]');
  for (var i = 0; i < scripts.length; i++){
    var script = scripts[i];
    var newScript = document.createElement("script");
    var attributes = script.attributes;
    for (var j = 0; j < attributes.length; j++){
      var attribute = attributes[j];
      if (attribute.name !== 'type' && attribute.name !== 'src') {
        newScript.setAttribute(attribute.name, attribute.value);
      }
    }
    var blob = new Blob([script.innerText], {
      type: 'application/javascript'
    });
    newScript.setAttribute("data-main", URL.createObjectURL(blob));
    newScript.src = "/lib/polyfills/shimport/shimport@2.0.5.dev.js";

    script.parentNode.insertBefore(newScript, script);
    script.remove();
  }
}

@Rich-Harris
Copy link
Member

Thanks — since a more comprehensive fix would take time, I think it makes sense to merge this to unblock you, but I added a comment to make a regression less likely — details in https://github.com/sveltejs/kit/pull/5417/files#r916857155

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

Successfully merging this pull request may close these issues.

3 participants