angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

index.html formatting breaks Universal SSR

Open dottodot opened this issue 8 years ago • 49 comments

Versions

Angular CLI: 1.6.1
Node: 8.9.1
OS: darwin x64
Angular: 5.1.1
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, platform-server, router
... service-worker

@angular/cli: 1.6.1
@angular-devkit/build-optimizer: 0.0.36
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.42
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.1
@schematics/angular: 0.1.11
typescript: 2.4.2
webpack-bundle-analyzer: 2.9.1
webpack: 3.10.0

Repro steps

Build the app

Observed behavior

After building the app and starting the express app and I can see I not getting SSR content. However if I open the compiled index.html and save it it Visual Code so that the html is formatted again the problem goes away. This suggests that the build process is somehow formatting the html in a way that's causing the issue, but I don't get any errors.

Also the other odd thing is if I removed the Service Worker I don't get the issue so I initially though that was the problem as reported here https://github.com/angular/angular/issues/20890

Desired behavior

Be able to include build app and have SSR.

I'm just very confused as to how the combination of the formatted html and service worker can be preventing ssr working.

Mention any other details that might be useful (optional)

I've tried removing all additions to my index.html to make sure nothing I've added causes the issue. I've only recently started having this issue but can't be sure of which version.

dottodot avatar Dec 14 '17 11:12 dottodot

below is my swprecacheconfig.json

module.exports = { navigateFallback: 'dist/browser/index.html', stripPrefix: 'dist/browser', root: 'dist/browser', staticFileGlobs: [

'dist/browser/index.html',
'dist/browser/**.js',
'dist/browser/**.css',
'dist/browser/**.ico',
'dist/browser/assets/images/**.jpg',
'dist/browser/assets/images/**.png',
'dist/browser/assets/images/**.gif',
'dist/browser/assets/js/**/**.js',
'dist/browser/assets/js/**.js',
'dist/browser/assets/css/**.css'

// 'dist/browser/assets/fonts/.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,ico}', // 'dist/browser/.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,ico}', // '!dist/browser/(Subscription)/**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,ico}' ],

runtimeCaching: [{ urlPattern: /^https://.s3..com//, handler: 'cacheFirst' }] };

@Priemar pls help me to fix it

kamalakkanni avatar May 21 '18 06:05 kamalakkanni

@kamalakkanni

i didnt used swprechache, so i dont know in detail how the generated code looks like, but if its similar to the ngsw build, your config looks good.

are you lazy loading module packages ? if yes are lazy load them at router level ? it looks like you are

for me it looks like the required module isnt in the cache. Means you loaded i.e. the main module. then you went offline. Now you navigate to a lazy route, and its shows an error bec. you try to lazy load the missing code.

if thats the case, we need to figure out why the serviceworker isnt prefetching the lazy modules.

in my case the serviceworker is prefetching all modules. What also could be a problem. If you load the main site. -> serviceworker starts prefetching the item. If you close your app to fast. The serviceworker maybe didnt prefetched all modules... that means they are not in the cache. If you go offline now and back to the app, you will get the same result, bec the modules are not cached.

Please check:

  1. do you use lazy load modules ..
  2. are these modules in the cache, before you go offline

Priemar avatar May 21 '18 10:05 Priemar

I didn't use lazy loading for modules.after route navigation also offline working fine.but not ssr in production server

kamalakkanni avatar May 21 '18 14:05 kamalakkanni

And thats the point what i mentioned in my first answer. The generated ngsw-serviceworker caches index.html. If im reloading the page, always this index.html is served from cache.

You can check if swprecache acts like ngsw. Remove all serviceworkers and clear the cache. Then navigate to your app. If you check the network traffic, you should see the initial page (url) is from the server and prerendered. -> thats should work.

in the meantime the serviceworker prefetches all defined resources. Please check the

index.html

this is your fallback page. -> this index html should also prerendered.

If you reload the app, no matter which route you take, the network traffic will tell you if the route is taken from cache. Which i expect is the prerendered index.html

If thats the case you have exactly the same behv. i got with ngsw. In my opinion SSR for those service-worker (ngsw, maybe swprecache) implementations is no implemented as expected.

Its working for the inital load. (thats works) But after page changes, the serviceworker should fetch the actual route from the server and caches this route. If you reload the page now, it should return the last cached route. Thats what i expected.

Theoretically its not difficult to implement a serviceworker which acts like this. But rewriting the whole generation logic and keep it uptodate is a lot of work.

Im not sure but i think swprecache allow you to write plugins. I dont know how this plugin mechanism works, but maybe you can write a plugin which fetches the last page after navigating in your app.

If you dont want to rewrite the serviceworker, i recommend using an appshell. I think thats a good mix of SSR and serviceworker, the page feels pretty fast, even not all informations are visible on refresh.

My case was a bit more tricky i needed to deliver different app-shells. One for the logged-in user and one for the not logged in once. So i had to give the serviceworker prefetch the information if the user is logged in or not (which is also not supported in the actual service worker implementations)

If i get the chance to get in contact with one of the angular core developers, i would discuss this and maybe we find a good solution on the base system. If there are not more ppl which need this behaviour to SSR PWAs i think this feature will not be available soon ^^ and the only solution is to write your own service-worker.

but ill keep you up to date, if there is something new. or if i found another solution for this.

sorry for the bad news

Priemar avatar May 21 '18 17:05 Priemar

Hi,

thanks for your help.can you pls tell me how to use ngswconfig.json if its working fine with ssr

Thanks

kamalakkanni avatar May 22 '18 06:05 kamalakkanni

You are welcome, i dont see any benefits switching to ngsw, for you, bec. it will act like your actual sw.

But here is the tutorial: https://angular.io/guide/service-worker-getting-started

Priemar avatar May 22 '18 08:05 Priemar

Hi,offline is working but not ssr in online.pls help me.If i changed server.ts as per your suggestion .Iam getting cannot get error and site is not loaded.I copied server.ts from https://github.com/ssatz/Angular-SSR-Service-Worker.SSR is working for some pages and not working for some pages. pls help me to fix it.thanks

How can I catch dynamic urls and exclude subsite url in ngsw caching

kamalakkanni avatar May 22 '18 10:05 kamalakkanni

That hint re: deleting the index.html hash from the ngsw-manifest output was worth gold, @Priemar !! Thank you so much.

Now I've got Universal & Service Worker working together in harmony <3

Really should be a proper way to do this though.

d2kx avatar May 27 '18 17:05 d2kx

ok, finally someone can have an full instruction for us to follow? please, too much step with error to try :(

hiepxanh avatar Jun 20 '18 07:06 hiepxanh

@D2KX but can your app work offline then?

vytautas-pranskunas- avatar Jul 05 '18 13:07 vytautas-pranskunas-

Anu progress on this one? I think this is extremly important for proper PWA

vytautas-pranskunas- avatar Jul 06 '18 08:07 vytautas-pranskunas-

@vytautas-pranskunas- sure the app will work offline, otherwise i wouldnt call it an pwa.

Priemar avatar Jul 07 '18 09:07 Priemar

No, I don't think that removing index.html from the generated ngsw.json would allow the app to work offline. I definitely do not recommend that.

As has been mentioned in some answers above, SSR works as expected on the first request (before the SW is activated). Once the SW takes over, the non-prerendered index.html is served, but that is OK, since it is served by the SW and doesn't require a round-trip to the server. (Offline mode also works fine in that case.)

This is by design: SSR speeds up the requests where no SW is in effect (including first-time visits, SEO, etc) and SW speeds up subsequent requests (and enables offline mode) by serving the cached files and scripts.

I am going to close this, since everything works as expected. Note: There are still issues with using app-shell. This is tracked in #8794.

gkalpak avatar Jul 09 '18 16:07 gkalpak

But I am sure you understand that ssr should refresh index content on every inner navigation, but if only first navigation is cached so it is not SEO friendly because no matter where bot navigates internally it gets same cached index? That's why SW is not compatible with SSR i think. Or?

vytautas-pranskunas- avatar Jul 09 '18 22:07 vytautas-pranskunas-

it is not SEO friendly because no matter where bot navigates internally it gets same cached index?

Again, SW is irrelevent for SEO. Web crawlers do not use ServiceWorkers. So, whether your app has ServiceWorker support or not, it doesn't make any difference for crawling the content (for indexing, previewing, etc.). For SEO, SSR works as expected.

For actual, human users accessing your app from the same device/browser, SSR speeds up the first ever request and then SW takes over and speeds up all subsequent requests. In that sense, they both work as expected and in tandem to improve the overall UX.

gkalpak avatar Jul 10 '18 07:07 gkalpak

O, but how if crawlers are hitting same files how do they bypass SW? SW is nothing else then javascript file or SW just gets registered itself in browser?

On Tue, Jul 10, 2018 at 9:56 AM George Kalpakas [email protected] wrote:

it is not SEO friendly because no matter where bot navigates internally it gets same cached index?

Again, SW is irrelevent for SEO. Web crawlers do not use ServiceWorkers. So, whether your app has ServiceWorker support or not, it doesn't make any difference for crawling the content (for indexing, previewing, etc.). For SEO, SSR works as expected.

For actual, human users accessing your app from the same device/browser, SSR speeds up the first ever request and then SW takes over and speeds up all subsequent requests. In that sense, they both work as expected and in tandem to improve the overall UX.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/angular/angular-cli/issues/8872#issuecomment-403734351, or mute the thread https://github.com/notifications/unsubscribe-auth/ADvMl4IGSDMbf49ATA_DdDCWD_YkZHbhks5uFF4lgaJpZM4RB7dX .

--

Pagarbiai,

Vytautas Pranskunas

vytautas-pranskunas- avatar Jul 10 '18 08:07 vytautas-pranskunas-

Again, SW is irrelevent for SEO. Web crawlers do not use ServiceWorkers. So, whether your app has ServiceWorker support or not, it doesn't make any difference for crawling the content (for indexing, previewing, etc.). For SEO, SSR works as expected.

For actual, human users accessing your app from the same device/browser, SSR speeds up the first ever request and then SW takes over and speeds up all subsequent requests. In that sense, they both work as expected and in tandem to improve the overall UX.<

I 100% agree with gkalpak.

What we discussed above:

  • You start your app for the first time. The first request goes to the server. The server returns a SSR document. (in the meantime the SW will cache all prefetch files (which are defined in the generated swconfig). This includes the inital document (also defined in the config) which is per default the index.html

the default implementation of the SSR server code will only render routes which are not documents. documents are returned as they are on the server.

that means if the SW loads the index.html and adds this file to the cache. This index.html is not prerendered.

If you now reload your app the SW will serve the default document (index.html) from the cache and this file is not prerendered.

resumee: the first app load will give you a fast prerendered version. The next reloads will always serve a not prerendered version.

I found a Dirty Workaround to force the server to return a prerendered index.html and the SW to cache it. as i mentioned above: I defintily would not recommend todo this, bec you need to change autogenerated files. Which you should never do, + there could be security issues, other sideffects etc.

Actually there is no solution with the NGSW implementation. Sure you can write your own SW etc. but normally time is money so i would recommend: change the design of your app by using "app-shell" etc...

writing your own SW is not worth the effort you have to put in.

@gkalpak :

No, I don't think that removing index.html from the generated ngsw.json would allow the app to work offline. I definitely do not recommend that.<

Yeah its working when u manipulated the ngsw.json, as i wrote above, bec the serviceworker is still caching and serving the index.html, but per default it will cache-bust the index.html bec. the hash of the prerendered index.html doesnt match the hash in the hashtable of the config (obvi.)

But as u said i would not recommend it.... definilty not

Priemar avatar Jul 10 '18 08:07 Priemar

@vytautas-pranskunas-

SW gets registred in the browser. YES Crawlers are always requesting informations directly from the server. (for every route) this means every "site / route" the crawler is requesting the server will return the SSR result (depending on the server code).

Priemar avatar Jul 10 '18 08:07 Priemar

OK thats makes sense but then I do not get why @Priemar needs workarounds? Because for user "view source" does not really matters

vytautas-pranskunas- avatar Jul 10 '18 08:07 vytautas-pranskunas-

@vytautas-pranskunas- bec. reloading the app where all files come from the SW. will show the unrendered "index.html" until the browser finished rendering. This are just milliseconds not more, but its still visble for the user.

So its not required but u know: everything for the best UserXP :)

keep in mind im against this workaround ... just wanna show why the acutal generated ngsw. acts like it does.

Priemar avatar Jul 10 '18 08:07 Priemar