Skip to content

Updates for differential serving.#121

Merged
aomarks merged 1 commit into2.0-preview-classfrom
2.0-preview-class-relative
May 12, 2017
Merged

Updates for differential serving.#121
aomarks merged 1 commit into2.0-preview-classfrom
2.0-preview-class-relative

Conversation

@aomarks
Copy link
Copy Markdown
Member

@aomarks aomarks commented May 11, 2017

We want to demonstrate differential serving using shop as an example where we serve a different build (e.g. bundled/unbundled) to different user agents based on their capabilities (e.g. push).

The strategy we have settled on (cc @graynorton) is to switch the entrypoint we serve depending on user agent, where each entrypoint has a <base> tag pointing to its build sub-directory, so that we can then use relative links to point at the right static resources.

So, this PR updates shop to use relative links for static resources, and adds a default <base href="/"> tag (which we will automatically update from polymer build) so that the app behaves exactly as before when served from the root.

It also sets the service worker scope to / so that (in conjunction with a Service-Worker-Allowed header), service workers served from a build's sub-directory can intercept requests from all routes.

@aomarks aomarks changed the title Relative URLs and <base> tag for differential serving. Updates to facilitate differential serving. May 11, 2017
@aomarks aomarks changed the title Updates to facilitate differential serving. Updates for differential serving. May 11, 2017
@aomarks aomarks requested a review from rictic May 11, 2017 05:33
<html lang="en">
<head>
<meta charset="utf-8">
<base href="/">
Copy link
Copy Markdown

@cdata cdata May 11, 2017

Choose a reason for hiding this comment

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

Worth noting that base can be set once per document. Its usage is typically considered esoteric, but we will be co-opting it for our purposes, possibly to the exclusion of someone else's.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good point. It's useful enough for this case, though, that I'm OK with usurping it. If someone has a different need for <base>, I think it's reasonable to put the onus on them to come up with a different scheme for differential serving.

// Register service worker if supported.
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js');
navigator.serviceWorker.register('service-worker.js', {scope: '/'});
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How does setting base in a document affect resource loading in a service worker? Suppose I have customized the service worker so that I can A/B test a specific component by redirecting requests for it to one of two candidate implementations. Will I need to be aware of the base configuration in order to correctly load the candidate implementation that has been chosen?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good question, @cdata . In our proposed scheme for multiple builds / differential serving, each individual build has its own service worker, presumably partially auto-generated by the build process (as is the case with our toolchain).

There is a small bit of server- or network-resident logic in this scheme that chooses one of the individual builds based on the visiting user agent, and based on that logic the service worker associated with that build is the one that gets registered.

In theory, any special SW logic you had for specific purposes would be essentially unaffected by our build-switching scheme; it would just need to find its way into each of the individual service workers in the final file layout.

Thoughts?

on-iron-form-response="_didReceiveResponse"
on-iron-form-presubmit="_willSendRequest">
<form method="post" action="/data/sample_success_response.json">
<form method="post" action="data/sample_success_response.json">
Copy link
Copy Markdown

@cdata cdata May 11, 2017

Choose a reason for hiding this comment

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

This is an interesting example. IRL, this API probably won't be static, and probably will live at a fixed location even though the client relies on differential serving. If I naively follow a "best practice" of never using absolute URLs for things (so as to take advantage of differential serving), it may be difficult to reason about the correctness of changes like this (which, again IRL, could actually be a bug). The base, in this case, creates a dissonance between the URLs for static resources and non-static ones (unless my server already fixes this for me in a magical way).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Agree, SHOP is not very representative here. Also agree there's a bit of nuance needed in explaining which URLs need to be relative to work in this scheme -- essentially it's URLs to static resources on the primary hosting server. URLs for navigating to "virtual routes" should still be expressed as absolute from the server root, and of course URLs referring to resources on other hosts will remain absolute.

@aomarks
Copy link
Copy Markdown
Member Author

aomarks commented May 12, 2017

Merging! (Talked with keanulee/frankiefu offline).

Thanks for the discussion. The other point keanulee brought up is that with this pattern we're duplicating static assets (images etc.) in multiple build directories, which is not ideal in terms of deploy size on disk, and also caching. It's conceivable that different builds would actually have different static files (e.g. higher image compression for a mobile build), but otherwise you would just use absolute URLs for shared assets, and lift them up a directory as a post-build step. That's something we could eventually add to the polymer tooling.

@aomarks aomarks merged commit 40088e1 into 2.0-preview-class May 12, 2017
@aomarks aomarks deleted the 2.0-preview-class-relative branch May 12, 2017 23:13
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