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

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.)

@Priemar, just removing the index.html entry from ngsw.json (as was suggested in https://github.com/angular/angular-cli/issues/8872#issuecomment-392350259) will break offline afaict.

Regarding your approach to cache prerendered index.html: Unless I am misunderstanding something, this will not work correctly when you have routes, because the same index.html will be rendered for all of them (which look like navigation requests to the SW). In the best case scenario, you will get a flash of irrelevant content (which is way worse imo than a delay of meaningful content :grin:).

gkalpak avatar Jul 10 '18 08:07 gkalpak

@gkalpak :

just removing the index.html entry from ngsw.json (as was suggested in #8872 (comment)) will break offline afaict.

you are right: you have to remove it form the hashtable. bec the ngsw code will check the hashvalue of the served files with the hashtable, and if this hashes are not equal (which is not possible bec how should you know at compilation time, what the hash of a dynamic prerendered file looks like). the sw will not cache this index.html and it will not work offline. ... so you have to force the sw to accept and cache the prerendered index.html (thats what i mean).

Regarding your approach to cache prerendered index.html: Unless I am misunderstanding something, this will not work correctly when you have routes, because the same index.html will be rendered for all of them (which look like navigation requests to the SW). In the best case scenario, you will get a flash of irrelevant content (which is way worse imo than a delay of meaningful content 😁).

and yes thats the next point i didnt mentioned yet. and there is no workaround for it ^^ theoretically the sw need to refresh the cached index.html on every route change ... which leads to other problems.

So all over all i think we both agree on not doing any workarounds and use the default mechanism, there is a reason why it acts like it does. To change this behaviour a completly different implementation is required. -> which leads to other disadvantages ...

in my opinion closing this issue is pretty good. there is no need to discuss a scenario (workaround) nobody should use :)

as i said, i just wanted to clearify why things work like they do with the default implementation.

cheers

Priemar avatar Jul 10 '18 09:07 Priemar

And how i can test crawlers behaviour that it get stright to SSR content?

On Tue, Jul 10, 2018 at 11:04 AM Priemar [email protected] wrote:

@gkalpak https://github.com/gkalpak :

just removing the index.html entry from ngsw.json (as was suggested in #8872 https://github.com/angular/angular-cli/issues/8872 (comment)) will break offline afaict.

you are right: you have to remove it form the hashtable. bec the ngsw code will check the hashvalue of the served files with the hashtable, and if this hashes are not equal (which is not possible bec how should you know at compilation time, what the hash of a dynamic prerendered file looks like). the sw will not cache this index.html and it will not work offline. ... so you have to force the sw to accept and cache the prerendered index.html (thats what i mean).

Regarding your approach to cache prerendered index.html: Unless I am misunderstanding something, this will not work correctly when you have routes, because the same index.html will be rendered for all of them (which look like navigation requests to the SW). In the best case scenario, you will get a flash of irrelevant content (which is way worse imo than a delay of meaningful content 😁).

and yes thats the next point i didnt mentioned yet. and there is no workaround for it ^^ theoretically the sw need to refresh the index.html on every route change ... which leads to other problems to solve.

So all over all i think we both agree on not doing any workarounds and use the default mechanism, there is a reason why it acts like it does. To change this behaviour a completly different implementation is required. -> which leads to other disadvantages ...

in my opinion closing this issue is pretty good. there is no need to discuss a scenario (workaround) nobody should use :)

as i said, i just wanted to clearify why things work like they do with the default implementation.

cheers

— 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-403753134, or mute the thread https://github.com/notifications/unsubscribe-auth/ADvMl_sURvZnc1jeOzjEOPMrLNQ50nPfks5uFG4ogaJpZM4RB7dX .

--

Pagarbiai,

Vytautas Pranskunas

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

@vytautas-pranskunas-

  • you can use google search console. there is an option to visualy show the crawler results.
  • or you use the network tab from the browser. Load your site url you wanna check. -> search the request in the network tab and click preview -> here u can see what the server returns (prerendered or not)

Priemar avatar Jul 10 '18 09:07 Priemar

But make sure the file was not served by the SW. There are several ways to achieve this for testing:

  • Build the app in non-production mode (SW is only included in production mode).
  • Remove serviceWorker: true from your angular.json config (or have a different build-target with serviceWorker: false.
  • Remove the generated ngsw.json from the built artifacts.
  • Disable SW in the browsers DevTools (e.g. in Chrome's DevTools, there is a Service Workers section in the Application tabs, that has a Bypass for network checkbox).

gkalpak avatar Jul 10 '18 09:07 gkalpak

So my question was that how hoe i see in action crawler going through different page routes and see that it gets content not from SW but from SSR. :)

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

But make sure the file was not served by the SW. There are several ways to achieve this for testing:

  • Build the app in non-production mode (SW is only included in production mode).
  • Remove serviceWorker: true from your angular.json config (or have a different build-target with serviceWorker: false.
  • Remove the generated ngsw.json from the built artifacts.
  • Disable SW in the browsers DevTools (e.g. in Chrome's DevTools, there is a Service Workers section in the Application tabs, that has a Bypass for network checkbox).

— 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-403763340, or mute the thread https://github.com/notifications/unsubscribe-auth/ADvMlwS6xbeXkk90pORtt_snaXwylTtbks5uFHaNgaJpZM4RB7dX .

--

Pagarbiai,

Vytautas Pranskunas

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

I have tried project from universal starter with enabled SW (followed by this issue: https://github.com/angular/angular/issues/23613#issuecomment-403525089) and i see no prerendered index.html even first one:

image

image

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

I have followed the steps outline in https://github.com/angular/angular/issues/23613#issuecomment-403525089 and I see prerendered content. You are probably not capturing the first request or deviating in some other way.

In any case, this has now moved into "support request" territory, for which this issue tracker is not suitable for. Please repost your issue on StackOverflow using tag angular.

If you are wondering why we don't resolve support issues via the issue tracker, please check out this explanation.

gkalpak avatar Jul 10 '18 13:07 gkalpak

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.