|
| 1 | +importtype{DevProgressSnapshot}from'./progress' |
| 2 | + |
| 3 | +/** |
| 4 | + * The scripts inlined into the dev server's loading and error pages. |
| 5 | + * |
| 6 | + * Each is serialised with `Function.prototype.toString()` and called with its |
| 7 | + * options, so it must stay self-contained: a reference to anything outside its |
| 8 | + * own body would be renamed by the bundler and undefined in the browser. Only |
| 9 | + * types may be imported here, since those are erased. |
| 10 | + */ |
| 11 | + |
| 12 | +exportinterfaceProgressClientOptions{ |
| 13 | +progressPath: string |
| 14 | +/** Id of the caption element, which doubles as the marker the poll looks for. */ |
| 15 | +captionId: string |
| 16 | +/** Custom property the loading bar's width is bound to. */ |
| 17 | +progressProperty: string |
| 18 | +/** How far the load had got when this page was served. */ |
| 19 | +elapsed: number |
| 20 | +/** How often to ask for the app once the server says it is ready. */ |
| 21 | +pollInterval: number |
| 22 | +} |
| 23 | + |
| 24 | +exportfunctionprogressClient(options: ProgressClientOptions): void{ |
| 25 | +constcaption=document.createElement('div') |
| 26 | +caption.id=options.captionId |
| 27 | +document.body.append(caption) |
| 28 | + |
| 29 | +letstart=Date.now()-options.elapsed |
| 30 | +letphase='' |
| 31 | + |
| 32 | +functionpaint(): void{ |
| 33 | +constseconds=`${((Date.now()-start)/1000).toFixed(1)}s` |
| 34 | +caption.textContent=phase ? `${phase} \u00B7 ${seconds}` : seconds |
| 35 | +} |
| 36 | + |
| 37 | +functionapply(snapshot: DevProgressSnapshot): void{ |
| 38 | +start=Date.now()-snapshot.elapsed |
| 39 | +phase=`${snapshot.phase} \u00B7 step ${snapshot.index+1}/${snapshot.total+1}` |
| 40 | +constpercent=Math.max(4,Math.round(snapshot.progress*100)) |
| 41 | +document.documentElement.style.setProperty(options.progressProperty,`${percent}%`) |
| 42 | +if(snapshot.message&&!document.title.startsWith(snapshot.message)){ |
| 43 | +document.title=snapshot.message |
| 44 | +} |
| 45 | +paint() |
| 46 | +} |
| 47 | + |
| 48 | +functionread(event: Event): DevProgressSnapshot|undefined{ |
| 49 | +try{ |
| 50 | +returnJSON.parse((eventasMessageEvent).data) |
| 51 | +} |
| 52 | +catch{ |
| 53 | +returnundefined |
| 54 | +} |
| 55 | +} |
| 56 | + |
| 57 | +setInterval(paint,100) |
| 58 | + |
| 59 | +constsource=newEventSource(options.progressPath) |
| 60 | + |
| 61 | +source.addEventListener('nuxt:loading',(event)=>{ |
| 62 | +constsnapshot=read(event) |
| 63 | +if(snapshot){ |
| 64 | +apply(snapshot) |
| 65 | +} |
| 66 | +}) |
| 67 | + |
| 68 | +// The dev server answers with the error page itself, which reports the failure |
| 69 | +// properly and recovers on its own, so there is nothing to render here. |
| 70 | +source.addEventListener('nuxt:error',()=>{ |
| 71 | +source.close() |
| 72 | +location.reload() |
| 73 | +}) |
| 74 | + |
| 75 | +source.addEventListener('nuxt:ready',(event)=>{ |
| 76 | +source.close() |
| 77 | +phase='starting the app' |
| 78 | +document.documentElement.style.setProperty(options.progressProperty,'100%') |
| 79 | +paint() |
| 80 | + |
| 81 | +// A reload leaves the caches warm, so there is nothing left to wait for. |
| 82 | +if(read(event)?.reload){ |
| 83 | +location.reload() |
| 84 | +return |
| 85 | +} |
| 86 | + |
| 87 | +// `nuxt:ready` means the request handler exists, not that it can render |
| 88 | +// quickly: on a cold start the first response still contends with the |
| 89 | +// warmup the server is finishing, and asking for it at once measured 2-3x |
| 90 | +// slower than asking a few hundred milliseconds later. So this page stays, |
| 91 | +// still reporting progress, and asks until the app answers. Doing that here |
| 92 | +// rather than leaving it to the template means every page the dev server |
| 93 | +// serves updates itself, so none of them needs a `Refresh` header whose |
| 94 | +// hard reload would throw the progress away every few seconds. |
| 95 | +constpoll=setInterval(()=>{ |
| 96 | +voidfetch(location.href,{headers: {accept: 'text/html'}}) |
| 97 | +.then(response=>response.text()) |
| 98 | +.then((body)=>{ |
| 99 | +if(!body.includes(options.captionId)){ |
| 100 | +clearInterval(poll) |
| 101 | +location.reload() |
| 102 | +} |
| 103 | +}) |
| 104 | +.catch(()=>{}) |
| 105 | +},options.pollInterval) |
| 106 | +}) |
| 107 | +} |
| 108 | + |
| 109 | +exportinterfaceRecoveryClientOptions{ |
| 110 | +progressPath: string |
| 111 | +} |
| 112 | + |
| 113 | +/** Reloads the error page as soon as the next load starts or succeeds. */ |
| 114 | +exportfunctionrecoveryClient(options: RecoveryClientOptions): void{ |
| 115 | +constsource=newEventSource(options.progressPath) |
| 116 | +constreload=(): void=>{ |
| 117 | +source.close() |
| 118 | +location.reload() |
| 119 | +} |
| 120 | +source.addEventListener('nuxt:ready',reload) |
| 121 | +source.addEventListener('nuxt:loading',reload) |
| 122 | +} |
| 123 | + |
| 124 | +/** Serialise a client function and its options into an inline `<script>`. */ |
| 125 | +exportfunctioninlineScript<T>(client: (options: T)=>void,options: T): string{ |
| 126 | +return`<script>(${client.toString()})(${JSON.stringify(options)})</script>` |
| 127 | +} |
0 commit comments