Skip to content

Commit 4eefc2f

Browse files
authored
Fixes for #298 #299 #300 in TransferStateService (#301)
* fix: use urlAfterRedirects to load prerendered content #299 prevent loading content for the page that user is not going to end up at but instead actually load content for page where user will end up after navigation * fix: fix loading of content for the root route #298 * fix: prevent flicker of content when navigating to the root route #300 * refactor(ng-lib): simplify #300 fix * fix(ng-lib): fallback to url when urlAfterRedirects is unspecified
1 parent 38905e8 commit 4eefc2f

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

projects/scullyio/ng-lib/src/lib/transfer-state/transfer-state.service.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
} from 'rxjs/operators';
1717
import {fetchHttp} from '../utils/fetchHttp';
1818
import {isScullyGenerated, isScullyRunning} from '../utils/isScully';
19+
import {mergePaths} from '../utils/merge-paths';
1920

2021
const SCULLY_SCRIPT_ID = `scully-transfer-state`;
2122
const SCULLY_STATE_START = `/** ___SCULLY_STATE_START___ */`;
@@ -60,7 +61,7 @@ export class TransferStateService {
6061
first()
6162
)
6263
),
63-
map((ev: NavigationEnd) => ev.url),
64+
map((ev: NavigationEnd) => ev.urlAfterRedirects || ev.url),
6465
shareReplay(1)
6566
);
6667

@@ -80,7 +81,10 @@ export class TransferStateService {
8081
} else if (isScullyGenerated()) {
8182
// On the client AFTER scully rendered it
8283
this.initialUrl = window.location.pathname || '__no_NO_no__';
83-
this.initialUrl = this.initialUrl.endsWith('/') ? this.initialUrl.slice(0, -1) : this.initialUrl;
84+
this.initialUrl =
85+
this.initialUrl !== '/' && this.initialUrl.endsWith('/')
86+
? this.initialUrl.slice(0, -1)
87+
: this.initialUrl;
8488
/** set the initial state */
8589
this.stateBS.next((window && window[SCULLY_SCRIPT_ID]) || {});
8690
}
@@ -140,7 +144,7 @@ export class TransferStateService {
140144
takeWhile(url => base(url) === this.currentBaseUrl),
141145
switchMap(url =>
142146
// Get the next route's page from the server
143-
fetchHttp<string>(url + '/index.html', 'text').catch(err => {
147+
fetchHttp<string>(mergePaths(url, '/index.html'), 'text').catch(err => {
144148
console.warn('Failed transfering state from route', err);
145149
return '';
146150
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import {mergePaths} from './merge-paths';
2+
3+
describe('mergePaths', () => {
4+
it('should return correct concatenated path', () => {
5+
expect(mergePaths('/about', '/index.html')).toBe('about/index.html');
6+
});
7+
8+
it('should return correct concatenated path when base ends with slash and path starts with slash', () => {
9+
expect(mergePaths('/about/', '/index.html')).toBe('about/index.html');
10+
});
11+
12+
it('should return correct concatenated path when base does not end with slash and path does not start with slash', () => {
13+
expect(mergePaths('/about', 'index.html')).toBe('about/index.html');
14+
});
15+
16+
it('should return correct concatenated path when base is a slash and path does not start with slash', () => {
17+
expect(mergePaths('/', '/index.html')).toBe('/index.html');
18+
});
19+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function mergePaths(base: string, path: string): string {
2+
if (base.endsWith('/') && path.startsWith('/')) {
3+
return `${base}${path.substr(1)}`;
4+
}
5+
if (!base.endsWith('/') && !path.startsWith('/')) {
6+
return `${base}/${path}`;
7+
}
8+
return `${base}${path}`;
9+
}

0 commit comments

Comments
 (0)