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

Fix Tapable.plugin warning and supports assetPrefix in register-sw #78

Merged
merged 4 commits into from Nov 16, 2018

Conversation

mirgj
Copy link
Contributor

@mirgj mirgj commented Nov 14, 2018

What's new

  • compiler.plugin(...) is deprecated and gives a warning during the next.js build; added in compiler.hooks.done as it's the suggested syntax.
  • Add assetPrefix supports has if you are fetching your assets from a CDN or virtual directory register-sw.js will always use the root folder to get the service-worker. With this fix will take either assetPrefix (if specified) or registerSwPrefix (in case we want to overwrite assetPrefix value)

@hanford
Copy link
Owner

hanford commented Nov 14, 2018

Wow awesome! Should be able to take a look either today or tomorrow! 🥂

let content = await readFile(require.resolve('./register-sw.js'), 'utf8');
content = content.replace('{REGISTER_SW_PREFIX}', registerSwPrefix);

await writeFile(join(__dirname, 'register-sw-compiled.js'), content, 'utf8');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not certain about this block because service workers must be served on the origin domain they're being used on.

So, the service worker on jackhanford.com would never register if it was being served on cdn.foobar.com.

w3c/ServiceWorker#940

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh, disregard ^ if this is for serving the service worker on a different path on the same domain. ie jackhanford.com/foo/bar/service-worker.js

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hanford yes it's actually to load it from a different path in case your application resides on a virtual directory

eg. my application is on test.com/food/bar but its trying to get the service worker from test.com/; this because the register script fetch the service-worker from the root folder.

plugin.js Outdated
} else {
compiler.plugin(
'done',
async () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think if we did async () => generateNextManifest(this.opts) the await is inferred

Copy link
Contributor Author

@mirgj mirgj Nov 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just used the previous syntax but I think it should be ok like that as well

Gonna try this out

plugin.js Outdated
compiler.hooks.done.tap(
'CopyPlugin',
async () => {
await generateNextManifest(this.opts);
Copy link
Owner

@hanford hanford Nov 14, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same with the comment above

if (compiler.hooks) {
compiler.hooks.done.tap(
'CopyPlugin',
async() => generateNextManifest(this.opts),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be ok now, tested and seems working well

@hanford
Copy link
Owner

hanford commented Nov 15, 2018

Thanks, I’ll get this merged and released tomorrow morning!

@mirgj
Copy link
Contributor Author

mirgj commented Nov 15, 2018

@hanford thanks you for the fast feedback 🥂

@hanford
Copy link
Owner

hanford commented Nov 15, 2018

@mirgj Really happy about this PR. The code works, I might tweak it a bit but overall this is good.

I am a little worried about doing this to every next-offline consumer that has an assetPrefix though.

Do you think this could warrant a new option so we don't need to make that assumption?

serviceWorkerPath?

cc @joaovieira have any thoughts?

@mirgj
Copy link
Contributor Author

mirgj commented Nov 16, 2018

@hanford I support another property which is registerSwPrefix that could be use for the same purpose (or you can also rename as you suggested). If you don't want to rely on assetPrefix one line of code should change to prevent using it by default: https://github.com/hanford/next-offline/pull/78/files#diff-168726dbe96b3ce427e7fedce31bb0bcR25

Anyway, honestly, I think whoever uses assetPrefix would experience this problem has this is how I discover the issue and that's the reason why I was supporting it. Anyway I got your point as maintainer you can choose what's the best solution for this case

@hanford
Copy link
Owner

hanford commented Nov 16, 2018

@mirgj my hesitation with supporting as assetPrefix is that I think a lot of people will get confused as to why SW's need to be served from the domain they're being registered on.

I think a lot of people are probably using assetPrefix for things like CDN's.

I'll get this merged and update the option to registerSwPrefix and cut a release in the morning tomorrow.

Thanks for the contribution! 🙌

Is it possible to serve service workers from CDN/remote origin?

@hanford hanford merged commit 41ed9b9 into hanford:master Nov 16, 2018
@hanford
Copy link
Owner

hanford commented Nov 16, 2018

registerSwPrefix is live in next-offline@3.1.0 Thanks again for the PR!

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.

None yet

2 participants