fix: only enable dev things in dev mode (#1789)
This commit is contained in:
@@ -9,3 +9,8 @@ export const DEBUG = !Deno.env.get("DENO_DEPLOYMENT_ID");
|
|||||||
export function bundleAssetUrl(path: string) {
|
export function bundleAssetUrl(path: string) {
|
||||||
return `${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}${path}`;
|
return `${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}${path}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export let DEV_MODE = false;
|
||||||
|
export function setDevMode(value: boolean) {
|
||||||
|
DEV_MODE = value;
|
||||||
|
}
|
||||||
|
|||||||
+11
-8
@@ -13,7 +13,12 @@ import {
|
|||||||
import { ComponentType, h } from "preact";
|
import { ComponentType, h } from "preact";
|
||||||
import * as router from "./router.ts";
|
import * as router from "./router.ts";
|
||||||
import { DenoConfig, Manifest } from "./mod.ts";
|
import { DenoConfig, Manifest } from "./mod.ts";
|
||||||
import { ALIVE_URL, JS_PREFIX, REFRESH_JS_URL } from "./constants.ts";
|
import {
|
||||||
|
ALIVE_URL,
|
||||||
|
JS_PREFIX,
|
||||||
|
REFRESH_JS_URL,
|
||||||
|
setDevMode,
|
||||||
|
} from "./constants.ts";
|
||||||
import { BUILD_ID, setBuildId } from "./build_id.ts";
|
import { BUILD_ID, setBuildId } from "./build_id.ts";
|
||||||
import DefaultErrorHandler from "./default_error_page.ts";
|
import DefaultErrorHandler from "./default_error_page.ts";
|
||||||
import {
|
import {
|
||||||
@@ -72,11 +77,6 @@ function isObject(value: unknown) {
|
|||||||
value !== null;
|
value !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isDevMode() {
|
|
||||||
// Env var is only set in prod (on Deploy).
|
|
||||||
return Deno.env.get("DENO_DEPLOYMENT_ID") === undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface StaticFile {
|
interface StaticFile {
|
||||||
/** The URL to the static file on disk. */
|
/** The URL to the static file on disk. */
|
||||||
localUrl: URL;
|
localUrl: URL;
|
||||||
@@ -118,7 +118,7 @@ export class ServerContext {
|
|||||||
plugins: Plugin[],
|
plugins: Plugin[],
|
||||||
configPath: string,
|
configPath: string,
|
||||||
jsxConfig: JSXConfig,
|
jsxConfig: JSXConfig,
|
||||||
dev: boolean = isDevMode(),
|
dev: boolean,
|
||||||
routerOptions: RouterOptions,
|
routerOptions: RouterOptions,
|
||||||
snapshot: BuildSnapshot | null = null,
|
snapshot: BuildSnapshot | null = null,
|
||||||
) {
|
) {
|
||||||
@@ -150,6 +150,10 @@ export class ServerContext {
|
|||||||
manifest: Manifest,
|
manifest: Manifest,
|
||||||
opts: FreshOptions & { skipSnapshot?: boolean; dev?: boolean },
|
opts: FreshOptions & { skipSnapshot?: boolean; dev?: boolean },
|
||||||
): Promise<ServerContext> {
|
): Promise<ServerContext> {
|
||||||
|
const dev = Deno.env.get("__FRSH_LEGACY_DEV") === "true" ||
|
||||||
|
Boolean(opts.dev);
|
||||||
|
setDevMode(dev);
|
||||||
|
|
||||||
// Get the manifest' base URL.
|
// Get the manifest' base URL.
|
||||||
const baseUrl = new URL("./", manifest.baseUrl).href;
|
const baseUrl = new URL("./", manifest.baseUrl).href;
|
||||||
|
|
||||||
@@ -442,7 +446,6 @@ export class ServerContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const dev = opts.dev ?? isDevMode();
|
|
||||||
if (dev) {
|
if (dev) {
|
||||||
// Ensure that debugging hooks are set up for SSR rendering
|
// Ensure that debugging hooks are set up for SSR rendering
|
||||||
await import("preact/debug");
|
await import("preact/debug");
|
||||||
|
|||||||
@@ -1,4 +1,12 @@
|
|||||||
import { waitForStyle, withPageName } from "$fresh/tests/test_utils.ts";
|
import { assertEquals, assertStringIncludes } from "./deps.ts";
|
||||||
|
import { Status } from "../server.ts";
|
||||||
|
import {
|
||||||
|
assertSelector,
|
||||||
|
fetchHtml,
|
||||||
|
waitForStyle,
|
||||||
|
withFresh,
|
||||||
|
withPageName,
|
||||||
|
} from "./test_utils.ts";
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "dev_command config",
|
name: "dev_command config",
|
||||||
@@ -25,3 +33,45 @@ Deno.test({
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("adds refresh script to html", async () => {
|
||||||
|
await withFresh("./tests/fixture/dev.ts", async (address) => {
|
||||||
|
const doc = await fetchHtml(address);
|
||||||
|
assertSelector(doc, `script[src="/_frsh/refresh.js"]`);
|
||||||
|
|
||||||
|
const res = await fetch(`${address}/_frsh/refresh.js`);
|
||||||
|
assertEquals(
|
||||||
|
res.headers.get("content-type"),
|
||||||
|
"application/javascript; charset=utf-8",
|
||||||
|
);
|
||||||
|
await res.body?.cancel();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("preact/debug is active in dev mode", async () => {
|
||||||
|
await withPageName(
|
||||||
|
"./tests/fixture_render_error/dev.ts",
|
||||||
|
async (page, address) => {
|
||||||
|
// SSR error is shown
|
||||||
|
const resp = await fetch(address);
|
||||||
|
const text = await resp.text();
|
||||||
|
assertEquals(resp.status, Status.InternalServerError);
|
||||||
|
assertStringIncludes(text, "Objects are not valid as a child");
|
||||||
|
|
||||||
|
await page.goto(address);
|
||||||
|
|
||||||
|
// Error page is shown with error message
|
||||||
|
const el = await page.waitForSelector(".frsh-error-page");
|
||||||
|
const text2 = await page.evaluate((el) => el.textContent, el);
|
||||||
|
assertStringIncludes(text2, "Objects are not valid as a child");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test("middleware destination internal", async () => {
|
||||||
|
await withFresh("./tests/fixture/dev.ts", async (address) => {
|
||||||
|
const resp = await fetch(`${address}/_frsh/refresh.js`);
|
||||||
|
assertEquals(resp.headers.get("destination"), "internal");
|
||||||
|
await resp.body?.cancel();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { ServerContext, Status } from "../server.ts";
|
import { ServerContext, Status } from "../server.ts";
|
||||||
import { REFRESH_JS_URL } from "../src/server/constants.ts";
|
|
||||||
import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
|
import { assert, assertEquals, assertStringIncludes } from "./deps.ts";
|
||||||
import manifest from "./fixture_error/fresh.gen.ts";
|
import manifest from "./fixture_error/fresh.gen.ts";
|
||||||
|
|
||||||
@@ -28,14 +27,3 @@ Deno.test("error page rendered", async () => {
|
|||||||
assertStringIncludes(body, `Error: boom!`);
|
assertStringIncludes(body, `Error: boom!`);
|
||||||
assertStringIncludes(body, `at render`);
|
assertStringIncludes(body, `at render`);
|
||||||
});
|
});
|
||||||
Deno.test("refresh.js rendered", async () => {
|
|
||||||
const resp = await router(
|
|
||||||
new Request("https://fresh.deno.dev" + REFRESH_JS_URL),
|
|
||||||
);
|
|
||||||
assert(resp);
|
|
||||||
assertEquals(resp.status, Status.OK);
|
|
||||||
assertEquals(
|
|
||||||
resp.headers.get("content-type"),
|
|
||||||
"application/javascript; charset=utf-8",
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|||||||
@@ -675,15 +675,6 @@ Deno.test({
|
|||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("middleware destination", async (t) => {
|
Deno.test("middleware destination", async (t) => {
|
||||||
await t.step("internal", async () => {
|
|
||||||
const resp = await handler(
|
|
||||||
new Request("https://fresh.deno.dev/_frsh/refresh.js"),
|
|
||||||
);
|
|
||||||
assert(resp);
|
|
||||||
assertEquals(resp.headers.get("destination"), "internal");
|
|
||||||
await resp.body?.cancel();
|
|
||||||
});
|
|
||||||
|
|
||||||
await t.step("static", async () => {
|
await t.step("static", async () => {
|
||||||
const resp = await handler(new Request("https://fresh.deno.dev/foo.txt"));
|
const resp = await handler(new Request("https://fresh.deno.dev/foo.txt"));
|
||||||
assert(resp);
|
assert(resp);
|
||||||
@@ -760,43 +751,6 @@ Deno.test("jsx pragma works", async (t) => {
|
|||||||
for await (const _ of lines) { /* noop */ }
|
for await (const _ of lines) { /* noop */ }
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("preact/debug is active in dev mode", async (t) => {
|
|
||||||
// Preparation
|
|
||||||
const { serverProcess, lines, address } = await startFreshServer({
|
|
||||||
args: ["run", "-A", "./tests/fixture_render_error/main.ts"],
|
|
||||||
});
|
|
||||||
|
|
||||||
await delay(100);
|
|
||||||
|
|
||||||
await t.step("SSR error is shown", async () => {
|
|
||||||
const resp = await fetch(address);
|
|
||||||
assertEquals(resp.status, Status.InternalServerError);
|
|
||||||
const text = await resp.text();
|
|
||||||
assertStringIncludes(text, "Objects are not valid as a child");
|
|
||||||
});
|
|
||||||
|
|
||||||
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
|
|
||||||
const page = await browser.newPage();
|
|
||||||
|
|
||||||
await page.goto(address, {
|
|
||||||
waitUntil: "networkidle2",
|
|
||||||
});
|
|
||||||
|
|
||||||
await t.step("error page is shown with error message", async () => {
|
|
||||||
const el = await page.waitForSelector(".frsh-error-page");
|
|
||||||
const text = await page.evaluate((el) => el.textContent, el);
|
|
||||||
assertStringIncludes(text, "Objects are not valid as a child");
|
|
||||||
});
|
|
||||||
|
|
||||||
await browser.close();
|
|
||||||
|
|
||||||
serverProcess.kill("SIGTERM");
|
|
||||||
await serverProcess.status;
|
|
||||||
|
|
||||||
// Drain the lines stream
|
|
||||||
for await (const _ of lines) { /* noop */ }
|
|
||||||
});
|
|
||||||
|
|
||||||
Deno.test("preloading javascript files", async () => {
|
Deno.test("preloading javascript files", async () => {
|
||||||
// Preparation
|
// Preparation
|
||||||
const { serverProcess, lines, address } = await startFreshServer({
|
const { serverProcess, lines, address } = await startFreshServer({
|
||||||
@@ -1002,10 +956,3 @@ Deno.test({
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
Deno.test("adds refresh script to html", async () => {
|
|
||||||
await withFresh("./tests/fixture/main.ts", async (address) => {
|
|
||||||
const doc = await fetchHtml(address);
|
|
||||||
assertSelector(doc, `script[src="/_frsh/refresh.js"]`);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|||||||
Reference in New Issue
Block a user