Skip to content

Commit 918e9ed

Browse files
committed
fix(prerender): fix prerender error handling
1 parent e7de19f commit 918e9ed

5 files changed

Lines changed: 45 additions & 16 deletions

File tree

src/compiler/prerender/prerender-app.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ async function runNextPrerenderUrl(config: d.Config, compilerCtx: d.CompilerCtx,
159159

160160
hydrateResults.push(results);
161161

162-
await writePrerenderDest(config, compilerCtx, outputTarget, results);
162+
if (results.diagnostics == null || results.diagnostics.length === 0) {
163+
await writePrerenderDest(config, compilerCtx, outputTarget, results);
164+
}
163165

164166
} catch (e) {
165167
// darn, idk, bad news
@@ -176,14 +178,16 @@ async function runNextPrerenderUrl(config: d.Config, compilerCtx: d.CompilerCtx,
176178

177179

178180
async function writePrerenderDest(config: d.Config, compilerCtx: d.CompilerCtx, outputTarget: d.OutputTargetWww, results: d.HydrateResults) {
179-
// create the full path where this will be saved
180-
const filePath = getWritePathFromUrl(config, outputTarget, results.url);
181+
if (typeof results.url === 'string' && typeof results.html === 'string') {
182+
// create the full path where this will be saved
183+
const filePath = getWritePathFromUrl(config, outputTarget, results.url);
181184

182-
// add the prerender html content it to our collection of
183-
// files that need to be saved when we're all ready
184-
await compilerCtx.fs.writeFile(filePath, results.html, { useCache: false });
185+
// add the prerender html content it to our collection of
186+
// files that need to be saved when we're all ready
187+
await compilerCtx.fs.writeFile(filePath, results.html, { useCache: false });
185188

186-
// write the files now
187-
// and since we're not using cache it'll free up its memory
188-
await compilerCtx.fs.commit();
189+
// write the files now
190+
// and since we're not using cache it'll free up its memory
191+
await compilerCtx.fs.commit();
192+
}
189193
}

src/core/host-snapshot.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ export const initHostSnapshot = (domApi: d.DomApi, cmpMeta: d.ComponentMeta, hos
3636
// but this browser doesn't support it
3737
// so let's polyfill a few things for the user
3838

39-
if ((window as any).HTMLElement && !('shadowRoot' in (window as any).HTMLElement.prototype)) {
40-
(hostElm as any).shadowRoot = hostElm;
41-
}
39+
try {
40+
if ((window as any).HTMLElement && !('shadowRoot' in (window as any).HTMLElement.prototype)) {
41+
(hostElm as any).shadowRoot = hostElm;
42+
}
43+
} catch (e) {}
4244
}
4345
}
4446

src/server/platform-server.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as d from '../declarations';
2+
import { catchError } from '../compiler/util';
23
import { createDomApi } from '../renderer/dom-api';
34
import { createQueueServer } from './queue-server';
45
import { createRendererPatch } from '../renderer/vdom/patch';
@@ -68,8 +69,12 @@ export function createPlatformServer(
6869
// V8 Context provides an isolated global environment
6970
config.sys.vm.createContext(compilerCtx, outputTarget, win);
7071

71-
// execute the global scripts (if there are any)
72-
runGlobalScripts();
72+
try {
73+
// execute the global scripts (if there are any)
74+
runGlobalScripts();
75+
} catch (e) {
76+
catchError(diagnostics, e);
77+
}
7378

7479
// internal id increment for unique ids
7580
let ids = 0;
@@ -308,7 +313,18 @@ export function createPlatformServer(
308313
return;
309314
}
310315

311-
config.sys.vm.runInContext(compilerCtx.appFiles.global, win);
316+
if (!win.matchMedia) {
317+
win.matchMedia = () => {
318+
return { matches: true };
319+
};
320+
}
321+
322+
const globalContext = Object.assign(win, {
323+
x: compilerCtx,
324+
r: Context.resourcesUrl
325+
});
326+
327+
config.sys.vm.runInContext(compilerCtx.appFiles.global, globalContext);
312328
}
313329

314330
function onError(err: Error, type: RUNTIME_ERROR, elm: d.HostElement, appFailure: boolean) {

src/sys/node/node-dom.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,11 @@ function polyfillJsDom(window: any) {
5858
};
5959
}
6060

61+
if (!window.matchMedia) {
62+
window.matchMedia = () => {
63+
return { matches: true
64+
};
65+
};
66+
}
67+
6168
}

src/testing/puppeteer/puppeteer-element.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ export class E2EElement extends MockElement implements pd.E2EElementInternal {
446446
return rtn;
447447
});
448448

449-
}, this._elmHandle, this._queuedActions);
449+
}, this._elmHandle, this._queuedActions as any);
450450

451451
this._queuedActions.length = 0;
452452

0 commit comments

Comments
 (0)