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 e00e934 commit f656799
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/funny-seahorses-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'astro': patch
'@astrojs/deno': patch
---

Deno SSR with prerender=true complains about invalid URL scheme
10 changes: 6 additions & 4 deletions packages/astro/src/core/build/plugins/plugin-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ export { _default as default };`;
`
: ''
}
export const adapter = _adapter
${adapter.name !== '@astrojs/deno' ? `
const _start = 'start';
if(_start in adapter) {
adapter[_start](_manifest, _args);
}`;
}
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.167.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);}`
14 changes: 12 additions & 2 deletions packages/integrations/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ 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;
serverEntry: string;
assets: string;
}

interface Options {
Expand All @@ -32,10 +32,12 @@ export function getAdapter(args?: Options): AstroAdapter {
export default function createIntegration(args?: Options): AstroIntegration {
let _buildConfig: BuildConfig;
let _vite: any;
let needsBuildConfig = false;
return {
name: '@astrojs/deno',
hooks: {
'astro:config:done': ({ setAdapter, config }) => {
needsBuildConfig = !config.build.client;
setAdapter(getAdapter(args));
_buildConfig = config.build;

Expand All @@ -46,6 +48,12 @@ export default function createIntegration(args?: Options): AstroIntegration {
);
}
},
'astro:build:start': ({ buildConfig }) => {
// Backwards compat
if (needsBuildConfig) {
_buildConfig = buildConfig;
}
},
'astro:build:setup': ({ vite, target }) => {
if (target === 'server') {
_vite = vite;
Expand All @@ -70,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 All @@ -87,7 +97,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
// Remove chunks, if they exist. Since we have bundled via esbuild these chunks are trash.
try {
const chunkFileNames =
_vite?.build?.rollupOptions?.output?.chunkFileNames ?? `chunks/chunk.[hash].mjs`;
_vite?.build?.rollupOptions?.output?.chunkFileNames ?? 'chunks/chunk.[hash].mjs';
const chunkPath = npath.dirname(chunkFileNames);
const chunksDirUrl = new URL(chunkPath + '/', _buildConfig.server);
await fs.promises.rm(chunksDirUrl, { recursive: true, force: true });
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.167.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 f656799

Please sign in to comment.