-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix prerendered page handling on Deno (#6284)
* fix(deno): handle prerendered pages * test(deno): add prerender test * fix: defensively access vite.build.rollupOptions.external * fix(deno): support other formats of rollupOptions.external * fix(deno): crawl prerendered files for match * fix(deno): ignore deno error in server file * fix(deno): cross-platform serve file
- Loading branch information
1 parent
ed92730
commit 61113dd
Showing
9 changed files
with
116 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@astrojs/deno': patch | ||
--- | ||
|
||
Fix prerendered page behavior |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// This file is a shim for any Deno-specific imports! | ||
// It will be replaced in the final Deno build. | ||
// | ||
// This allows us to prerender pages in Node. | ||
export class Server { | ||
listenAndServe() {} | ||
} | ||
|
||
export function serveFile() {} | ||
export function fromFileUrl() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,7 @@ import type { SSRManifest } from 'astro'; | |
import { App } from 'astro/app'; | ||
|
||
// @ts-ignore | ||
import { Server } from 'https://deno.land/[email protected]/http/server.ts'; | ||
// @ts-ignore | ||
import { fetch } from 'https://deno.land/x/file_fetch/mod.ts'; | ||
import { Server, serveFile, fromFileUrl } from '@astrojs/deno/__deno_imports.js'; | ||
|
||
interface Options { | ||
port?: number; | ||
|
@@ -16,6 +14,17 @@ interface Options { | |
let _server: Server | undefined = undefined; | ||
let _startPromise: Promise<void> | undefined = undefined; | ||
|
||
async function* getPrerenderedFiles(clientRoot: URL): AsyncGenerator<URL> { | ||
// @ts-ignore | ||
for await (const ent of Deno.readDir(clientRoot)) { | ||
if (ent.isDirectory) { | ||
yield* getPrerenderedFiles(new URL(`./${ent.name}/`, clientRoot)) | ||
} else if (ent.name.endsWith('.html')) { | ||
yield new URL(`./${ent.name}`, clientRoot) | ||
} | ||
} | ||
} | ||
|
||
export function start(manifest: SSRManifest, options: Options) { | ||
if (options.start === false) { | ||
return; | ||
|
@@ -40,7 +49,24 @@ export function start(manifest: SSRManifest, options: Options) { | |
// try to fetch a static file instead | ||
const url = new URL(request.url); | ||
const localPath = new URL('./' + app.removeBase(url.pathname), clientRoot); | ||
const fileResp = await fetch(localPath.toString()); | ||
|
||
let fileResp = await serveFile(request, fromFileUrl(localPath)); | ||
|
||
// Attempt to serve `index.html` if 404 | ||
if (fileResp.status == 404) { | ||
let fallback; | ||
for await (const file of getPrerenderedFiles(clientRoot)) { | ||
const pathname = file.pathname.replace(/\/(index)?\.html$/, ''); | ||
if (localPath.pathname.endsWith(pathname)) { | ||
fallback = file; | ||
break; | ||
} | ||
} | ||
if (fallback) { | ||
fileResp = await serveFile(request, fromFileUrl(fallback)); | ||
} | ||
} | ||
|
||
|
||
// If the static file can't be found | ||
if (fileResp.status == 404) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/integrations/deno/test/fixtures/basics/src/pages/prerender.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
export const prerender = true; | ||
--- | ||
|
||
<html> | ||
<body> | ||
<h1>test</h1> | ||
</body> | ||
</html> |