Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryWu1234 committed Feb 14, 2023
1 parent 60c8502 commit e2b6c45
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/odd-owls-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/deno': patch
---

fix: Deno SSR with prerender=true complains about invalid URL scheme
18 changes: 11 additions & 7 deletions packages/astro/src/core/build/vite-plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function vitePluginSSR(internals: BuildInternals, adapter: AstroAdapter):
},
load(id) {
if (id === resolvedVirtualModuleId) {
return `import * as adapter from '${adapter.serverEntrypoint}';
return `import * as _adapter from '${adapter.serverEntrypoint}';
import * as _main from '${pagesVirtualModuleId}';
import { deserializeManifest as _deserializeManifest } from 'astro/app';
const _manifest = Object.assign(_deserializeManifest('${manifestReplace}'), {
Expand All @@ -45,7 +45,7 @@ const _args = ${adapter.args ? JSON.stringify(adapter.args) : 'undefined'};
${
adapter.exports
? `const _exports = adapter.createExports(_manifest, _args);
? `const _exports = _adapter.createExports(_manifest, _args);
${adapter.exports
.map((name) => {
if (name === 'default') {
Expand All @@ -59,11 +59,15 @@ export { _default as default };`;
`
: ''
}
const _start = 'start';
if(_start in adapter) {
adapter[_start](_manifest, _args);
}`;
}
export const adapter = _adapter;
${adapter.name !== '@astrojs/deno' ? `
const _start = 'start';
if(_start in _adapter) {
_adapter[_start](_manifest, _args);
}`:
''} `;
}
return void 0;
},
async generateBundle(_opts, bundle) {
Expand Down
2 changes: 2 additions & 0 deletions packages/integrations/deno/src/code-constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DEFAULTIMPORT = `import { Server } from 'https://deno.land/std@0.132.0/http/server.ts';\n import { fetch } from 'https://deno.land/x/file_fetch/mod.ts';`
export const DEFAULTSTART = `const _start = 'start'; \n if(_start in _adapter) { \n adapter[_start](_manifest, _args);}`
3 changes: 3 additions & 0 deletions packages/integrations/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import esbuild from 'esbuild';
import * as fs from 'fs';
import * as npath from 'path';
import { fileURLToPath } from 'url';
import * as CONSTANT from './code-constant'

interface BuildConfig {
server: URL;
Expand Down Expand Up @@ -77,6 +78,8 @@ export default function createIntegration(args?: Options): AstroIntegration {
'astro:build:done': async () => {
const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server);
const pth = fileURLToPath(entryUrl);
const content = await fs.readFileSync(pth, 'utf8')
await fs.writeFileSync(pth, `${CONSTANT.DEFAULTIMPORT}\n${content}\n${CONSTANT.DEFAULTSTART}`);
await esbuild.build({
target: 'es2020',
platform: 'browser',
Expand Down
12 changes: 7 additions & 5 deletions packages/integrations/deno/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
// Normal Imports
import type { SSRManifest } from 'astro';
import { App } from 'astro/app';
// @ts-ignore
import { Server } from 'https://deno.land/std@0.132.0/http/server.ts';
// @ts-ignore
import { fetch } from 'https://deno.land/x/file_fetch/mod.ts';

interface Options {
port?: number;
hostname?: string;
start?: boolean;
}

// @ts-ignore
let _server: Server | undefined = undefined;
let _startPromise: Promise<void> | undefined = undefined;

Expand Down Expand Up @@ -39,7 +36,11 @@ 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());
const fileResp = await fetch(
localPath.toString().endsWith('client/')
? `${localPath.toString()}index.html`
: localPath.toString()
);

// If the static file can't be found
if (fileResp.status == 404) {
Expand All @@ -60,6 +61,7 @@ export function start(manifest: SSRManifest, options: Options) {
};

const port = options.port ?? 8085;
// @ts-ignore
_server = new Server({
port,
hostname: options.hostname ?? '0.0.0.0',
Expand Down

0 comments on commit e2b6c45

Please sign in to comment.