Skip to content

Commit

Permalink
fix(ng-lib): don't error when routing from non-Scully page to Scully-…
Browse files Browse the repository at this point in the history
…genreated page (#1541)
  • Loading branch information
SanderElias authored Feb 18, 2022
1 parent 855a418 commit b8d5a14
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
40 changes: 30 additions & 10 deletions libs/ng-lib/src/lib/transfer-state/transfer-state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@ import { DOCUMENT } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Inject, Injectable } from '@angular/core';
import { GuardsCheckEnd, NavigationStart, Router } from '@angular/router';
import { BehaviorSubject, catchError, filter, first, firstValueFrom, map, NEVER, Observable, of, pluck, shareReplay, switchMap, takeWhile, tap } from 'rxjs';
import {
BehaviorSubject,
catchError,
filter,
first,
firstValueFrom,
map,
NEVER,
Observable,
of,
pluck,
shareReplay,
switchMap,
takeWhile,
tap,
} from 'rxjs';
import { basePathOnly } from '../utils/basePathOnly';
import { isScullyGenerated, isScullyRunning } from '../utils/isScully';
import { mergePaths } from '../utils/merge-paths';
Expand Down Expand Up @@ -65,7 +80,7 @@ export class TransferStateService {
shareReplay(1)
);

constructor(@Inject(DOCUMENT) private document: Document, private router: Router, private http: HttpClient) { }
constructor(@Inject(DOCUMENT) private document: Document, private router: Router, private http: HttpClient) {}

startMonitoring() {
if (window && window['ScullyIO-injected'] && window['ScullyIO-injected'].inlineStateOnly) {
Expand All @@ -84,12 +99,15 @@ export class TransferStateService {
this.stateBS.next(exposed.transferState);
this.saveState(exposed.transferState);
}
} else if (isScullyGenerated()) {
// On the client AFTER scully rendered it
} else {
// On the client AFTER Scully rendered it. Also store the state in case the user comes from a non-scully page
this.initialUrl = window.location.pathname || '__no_NO_no__';
this.initialUrl = this.initialUrl !== '/' && this.initialUrl.endsWith('/') ? this.initialUrl.slice(0, -1) : this.initialUrl;
/** set the initial state */
this.stateBS.next((window && window[SCULLY_SCRIPT_ID]) || {});
if (isScullyGenerated()) {
/** only update the initial state when the page is Scully generated */
this.stateBS.next((window && window[SCULLY_SCRIPT_ID]) || {});
}
}
}

Expand All @@ -105,7 +123,7 @@ export class TransferStateService {
// Welp! ${this.script}
// --------------------------------------------------
// `)
this.document.body.insertBefore(this.script, last)
this.document.body.insertBefore(this.script, last);
}

/**
Expand Down Expand Up @@ -235,10 +253,12 @@ export class TransferStateService {
}

private readFromIndex(url): Promise<object> {
return firstValueFrom(this.http.get(dropPreSlash(mergePaths(url, '/index.html')), { responseType: 'text' })).then((html: string) => {
const newStateStr = html.split(SCULLY_STATE_START)[1].split(SCULLY_STATE_END)[0];
return JSON.parse(unescapeHtml(newStateStr));
});
return firstValueFrom(this.http.get(dropPreSlash(mergePaths(url, '/index.html')), { responseType: 'text' })).then(
(html: string) => {
const newStateStr = html.split(SCULLY_STATE_START)[1].split(SCULLY_STATE_END)[0];
return JSON.parse(unescapeHtml(newStateStr));
}
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"build:uni": "npm run scully -- --project sps-sample --RSD --tds --scan --ks",
"build:docs": "npm run scully -- --project scully-docs --RSD --404=index --scan --ks",
"scully": "node ./dist/libs/scully/src/scully.js",
"test": "run-s build:code symlinks server:kill build:docs server:kill build:sample jest:test cypress:e2e",
"test": "run-s build:code symlinks server:kill build:docs server:kill build:sample jest:test cypress:e2e:server",
"test:noBuild": "run-s server:kill build:docs server:kill build:sample jest:test cypress:e2e",
"lint": "nx workspace-lint && nx lint",
"server:wait": "wait-on tcp:1668",
Expand Down Expand Up @@ -204,4 +204,4 @@
"git add"
]
}
}
}

0 comments on commit b8d5a14

Please sign in to comment.