Skip to content

Commit 9c7b0ca

Browse files
committed
fix(prerender): router compatible with prerender
1 parent 053c375 commit 9c7b0ca

File tree

9 files changed

+2045
-2013
lines changed

9 files changed

+2045
-2013
lines changed

core/package-lock.json

Lines changed: 1991 additions & 1991 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/scripts/theme-builder/src/components/theme-builder/theme-builder.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import { DATA_URL, STORED_DEMO_MODE_KEY, STORED_DEMO_URL_KEY } from '../helpers'
99
})
1010
export class ThemeBuilder {
1111

12-
@State() cssText: string = '';
12+
@State() cssText = '';
1313
demoData: { name: string, url: string }[];
1414
@State() demoMode: string;
1515
@State() demoUrl: string;
1616
@State() hoverProperty: string;
1717
@State() propertiesUsed: string[];
1818
themeData: { name: string }[];
19-
@State() themeName: string = '';
19+
@State() themeName = '';
2020

2121
componentWillLoad () {
2222
return fetch(DATA_URL).then(rsp => {

core/src/components.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ declare global {
856856
/**
857857
* The type of the button. Possible values are: `"submit"`, `"reset"` and `"button"`. Default value is: `"button"`
858858
*/
859-
'type': string;
859+
'type': 'submit' | 'reset' | 'button';
860860
}
861861
}
862862

@@ -934,7 +934,7 @@ declare global {
934934
/**
935935
* The type of the button. Possible values are: `"submit"`, `"reset"` and `"button"`. Default value is: `"button"`
936936
*/
937-
'type'?: string;
937+
'type'?: 'submit' | 'reset' | 'button';
938938
}
939939
}
940940
}

core/src/components/button/button.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class Button {
8888
* Possible values are: `"submit"`, `"reset"` and `"button"`.
8989
* Default value is: `"button"`
9090
*/
91-
@Prop() type = 'button';
91+
@Prop() type: 'submit' | 'reset' | 'button' = 'button';
9292

9393
/**
9494
* Emitted when the button has focus.

core/src/components/gesture/gesture.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class Gesture {
1515

1616
private detail: GestureDetail;
1717
private positions: number[] = [];
18-
private gesture!: GestureDelegate;
18+
private gesture?: GestureDelegate;
1919
private lastTouch = 0;
2020
private pan!: PanRecognizer;
2121
private hasCapturedPan = false;
@@ -27,6 +27,7 @@ export class Gesture {
2727
@Prop({ connect: 'ion-gesture-controller' }) gestureCtrl!: HTMLIonGestureControllerElement;
2828
@Prop({ context: 'queue' }) queue!: QueueController;
2929
@Prop({ context: 'enableListener' }) enableListener!: EventListenerEnable;
30+
@Prop({ context: 'isServer' }) isServer!: boolean;
3031

3132
@Prop() disabled = false;
3233
@Prop() attachTo: string | HTMLElement = 'child';
@@ -65,6 +66,9 @@ export class Gesture {
6566
}
6667

6768
async componentWillLoad() {
69+
if (this.isServer) {
70+
return;
71+
}
6872
this.gesture = await this.gestureCtrl.create({
6973
name: this.gestureName,
7074
priority: this.gesturePriority,
@@ -73,6 +77,9 @@ export class Gesture {
7377
}
7478

7579
componentDidLoad() {
80+
if (this.isServer) {
81+
return;
82+
}
7683
// in this case, we already know the GestureController and Gesture are already
7784
// apart of the same bundle, so it's safe to load it this way
7885
// only create one instance of GestureController, and reuse the same one later
@@ -91,7 +98,9 @@ export class Gesture {
9198
this.blocker.destroy();
9299
this.blocker = undefined;
93100
}
94-
this.gesture.destroy();
101+
if (this.gesture) {
102+
this.gesture.destroy();
103+
}
95104
}
96105

97106
@Watch('disabled')
@@ -267,7 +276,7 @@ export class Gesture {
267276
}
268277

269278
private tryToCapturePan(): boolean {
270-
if (!this.gesture.capture()) {
279+
if (this.gesture && !this.gesture.capture()) {
271280
return false;
272281
}
273282
this.hasCapturedPan = true;

core/src/components/menu/menu.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,11 @@ export class Menu {
126126
if (this.type == null) {
127127
this.type = this.mode === 'ios' ? 'reveal' : 'overlay';
128128
}
129-
this.menuCtrl = await this.lazyMenuCtrl.componentOnReady();
129+
if (this.isServer) {
130+
this.disabled = true;
131+
} else {
132+
this.menuCtrl = await this.lazyMenuCtrl.componentOnReady();
133+
}
130134
}
131135

132136
componentDidLoad() {

core/src/components/router/router.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,37 @@ export class Router {
2525
@Prop({ context: 'config' }) config!: Config;
2626
@Prop({ context: 'queue' }) queue!: QueueController;
2727
@Prop({ context: 'window' }) win!: Window;
28+
@Prop({ context: 'isServer' }) isServer!: boolean;
2829

2930
@Prop() base = '';
3031
@Prop() useHash = true;
3132

3233
@Event() ionRouteChanged!: EventEmitter<RouterEventDetail>;
3334

34-
componentDidLoad() {
35-
this.init = true;
36-
console.debug('[ion-router] router did load');
35+
componentWillLoad() {
36+
console.debug('[ion-router] router will load');
3737

3838
const tree = readRoutes(this.el);
3939
this.routes = flattenRouterTree(tree);
4040
this.redirects = readRedirects(this.el);
4141

42-
// TODO: use something else
43-
requestAnimationFrame(() => {
44-
this.historyDirection();
45-
this.writeNavStateRoot(this.getPath(), RouterDirection.None);
46-
});
42+
return this.writeNavStateRoot(this.getPath(), RouterDirection.None);
43+
}
44+
45+
componentDidLoad() {
46+
this.init = true;
47+
48+
console.debug('[ion-router] router did load');
49+
50+
// const tree = readRoutes(this.el);
51+
// this.routes = flattenRouterTree(tree);
52+
// this.redirects = readRedirects(this.el);
53+
54+
// // TODO: use something else
55+
// requestAnimationFrame(() => {
56+
// this.historyDirection();
57+
// this.writeNavStateRoot(this.getPath(), RouterDirection.None);
58+
// });
4759
}
4860

4961
@Listen('ionRouteRedirectChanged')

core/src/utils/platform.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function isTablet(win: Window) {
3535
}
3636

3737
export function isDevice(win: Window) {
38-
return win.matchMedia('(any-pointer:coarse)').matches;
38+
return matchMedia(win, '(any-pointer:coarse)');
3939
}
4040

4141
export function isHybrid(win: Window) {
@@ -63,3 +63,9 @@ export function needInputShims(win: Window) {
6363
export function testUserAgent(win: Window, expr: RegExp) {
6464
return expr.test(win.navigator.userAgent);
6565
}
66+
67+
export function matchMedia(win: Window, query: string, fallback = false): boolean {
68+
return win.matchMedia
69+
? win.matchMedia(query).matches
70+
: fallback;
71+
}

core/src/utils/show-hide-when-utils.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isAndroid, isCordova, isElectron, isIOS, isIpad, isIphone, isPhablet, isTablet } from './platform';
1+
import { isAndroid, isCordova, isElectron, isIOS, isIpad, isIphone, isPhablet, isTablet, matchMedia } from './platform';
22
import { Config, Mode } from '../interface';
33

44
export function updateTestResults(displayWhen: DisplayWhen) {
@@ -29,7 +29,7 @@ export function isSizeMatch(win: Window, multiSizeString: string) {
2929
const sizes = multiSizeString.replace(/\s/g, '').split(',');
3030
for (const size of sizes) {
3131
const mediaQuery = SIZE_TO_MEDIA[size];
32-
if (mediaQuery && win.matchMedia(mediaQuery).matches) {
32+
if (mediaQuery && matchMedia(win, mediaQuery)) {
3333
return true;
3434
}
3535
}
@@ -39,7 +39,7 @@ export function isSizeMatch(win: Window, multiSizeString: string) {
3939
export function getTestResult(displayWhen: DisplayWhen) {
4040
const resultsToConsider: boolean[] = [];
4141
if (displayWhen.mediaQuery) {
42-
resultsToConsider.push(displayWhen.win.matchMedia(displayWhen.mediaQuery).matches);
42+
resultsToConsider.push(matchMedia(displayWhen.win, displayWhen.mediaQuery));
4343
}
4444
if (displayWhen.size) {
4545
resultsToConsider.push(isSizeMatch(displayWhen.win, displayWhen.size));
@@ -80,9 +80,10 @@ export function isOrientationMatch(win: Window, orientation: string) {
8080
}
8181

8282
export function isPortrait(win: Window): boolean {
83-
return win.matchMedia('(orientation: portrait)').matches;
83+
return matchMedia(win, '(orientation: portrait)');
8484
}
8585

86+
8687
const SIZE_TO_MEDIA: any = {
8788
'xs': '(min-width: 0px)',
8889
'sm': '(min-width: 576px)',

0 commit comments

Comments
 (0)