Skip to content
This repository was archived by the owner on Oct 12, 2021. It is now read-only.

feat(AppShell): add runtime parser #54

Merged
merged 2 commits into from
Jun 14, 2016
Merged

feat(AppShell): add runtime parser #54

merged 2 commits into from
Jun 14, 2016

Conversation

mgechev
Copy link
Member

@mgechev mgechev commented Jun 7, 2016

The Angular DI is dropped in this implementation for achieving minimal bundle size.

Code example:

var ngShellParser;

const SHELL_PARSER_CACHE_NAME = 'bar';
const APP_SHELL_URL = 'shell.html';
const NO_RENDER_CSS_SELECTOR = '[no-render]';
const ROUTE_DEFINITIONS = [
      '/bar.html',
      '/index.html',
      '/profile/:id'
    ];

self.addEventListener('install', function (event) {
  const parser = System.import('app-shell/index')
        .then(module => {
          ngShellParser = module.shellParserFactory({
            SHELL_PARSER_CACHE_NAME,
            APP_SHELL_URL,
            NO_RENDER_CSS_SELECTOR,
            ROUTE_DEFINITIONS
          });
          return ngShellParser;
        })
        .then(() => ngShellParser.fetchDoc())
        .then(res => ngShellParser.parseDoc(res))
        .then(strippedResponse => {
          return caches.open(SHELL_PARSER_CACHE_NAME)
            .then(cache => {
              return cache.put(APP_SHELL_URL, strippedResponse);
            });
        })
        .catch(e => {
          console.error(e);
        });
  event.waitUntil(parser);
});

self.addEventListener('fetch', function(event) {
  event.respondWith(
    ngShellParser.match(event.request)
      .then(response => {
        if (response) return response;
        return caches.match(event.request)
          .then(function(response) {
            if (response) {
              return response;
            }
            return fetch(event.request);
          })
      })
  );
});

@mgechev mgechev force-pushed the runtime-parser branch 4 times, most recently from 9b5b771 to d7b732d Compare June 10, 2016 17:45
@mgechev mgechev force-pushed the runtime-parser branch 2 times, most recently from 10ea047 to 33c61e2 Compare June 13, 2016 18:05
</html>
`;

const expectedResponse1_1 = `
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you give these more descriptive names?

@jeffbcross
Copy link
Contributor

I reviewed it as well as I could, with the limitations of how much information I can keep in my mind at one time. I added a couple of nits. You can go ahead and merge once those comments are taken care of.

Some follow-ups after this PR:

  • Add e2e tests using the tooling @alxhub is adding in Initial implementation of Service Worker testing #61
  • Case-sensitive-capable parser
  • Add a quick recipe showing how to use this, inside a new folder at the root of the project, mobile-toolkit/cookbook/parse-app-shell-from-universal.md. Eventually, we'll have a cookbook on mobile.angular.io based on recipes in this folder.

@mgechev
Copy link
Member Author

mgechev commented Jun 14, 2016

Everything is fixed.

@mgechev mgechev merged commit 9cc156d into master Jun 14, 2016
@mgechev mgechev deleted the runtime-parser branch June 14, 2016 13:56
@mgechev
Copy link
Member Author

mgechev commented Jun 15, 2016

I added some updates about the case sensitivity of the parser #60.

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

Successfully merging this pull request may close these issues.

2 participants