Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

previews exported files, adds preview tests #1766

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {FSWatcher, WatchEventType} from "node:fs";
import {access, constants} from "node:fs/promises";
import {createServer} from "node:http";
import type {IncomingMessage, RequestListener, Server, ServerResponse} from "node:http";
import {basename, dirname, join, normalize} from "node:path/posix";
import {basename, dirname, extname, join, normalize} from "node:path/posix";
import {difference} from "d3-array";
import type {PatchItem} from "fast-array-diff";
import {getPatch} from "fast-array-diff";
Expand Down Expand Up @@ -184,6 +184,8 @@ export class PreviewServer {
} else {
if ((pathname = normalize(pathname)).startsWith("..")) throw new Error("Invalid path: " + pathname);

const ext = extname(pathname).replace(/^\.html$/, "");

// Normalize the pathname (e.g., adding ".html" or removing ".html"
// based on preserveExtension) and redirect if necessary.
const normalizedPathname = encodeURI(config.normalizePath(pathname));
Expand All @@ -197,7 +199,7 @@ export class PreviewServer {
// request represents a JavaScript embed (such as /chart.js), and takes
// precedence over any page (such as /chart.js.md). Generate a wrapper
// module that allows this JavaScript module to be embedded remotely.
if (pathname.endsWith(".js")) {
if (ext === ".js") {
try {
end(req, res, await renderModule(root, pathname), "text/javascript");
return;
Expand All @@ -206,6 +208,20 @@ export class PreviewServer {
}
}

// If an export asset (such as /robots.txt or /embed/[param].svg) exists
// for this path, send it. It takes priority over a page robots.txt.md.
if (ext && ext !== ".md") {
const file = loaders.find(pathname);
if (file) {
try {
send(req, join(root, await file.load())).pipe(res);
return;
} catch (error) {
if (!isEnoent(error)) throw error;
}
}
}

// If this path ends with a slash, then add an implicit /index to the
// end of the path. Otherwise, remove the .html extension (we use clean
// paths as the internal canonical representation; see normalizePage).
Expand Down
1 change: 1 addition & 0 deletions test/preview/dashboard/asset.txt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.stdout.write(`Built by ${import.meta.url}`);
2 changes: 2 additions & 0 deletions test/preview/dashboard/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *
Disallow: /
24 changes: 23 additions & 1 deletion test/preview/preview-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ describe("preview server", () => {
expect(res.text).to.have.string("This text is not visible by default.");
});

// TODO - tests for /_observablehq and data loader requests
it("serves scripts from _observablehq/", async () => {
const res = await chai.request(testServerUrl).get("/_observablehq/stdlib.js");
expect(res).to.have.status(200);
expect(res.text).to.have.string("function FileAttachment");
});

it("serves local imports", async () => {
const res = await chai.request(testServerUrl).get("/_import/format.js");
Expand All @@ -68,9 +72,27 @@ describe("preview server", () => {
assert.ok(res.text);
});

it("serves files built with a data loader", async () => {
const res = await chai.request(testServerUrl).get("/_file/asset.txt");
expect(res).to.have.status(200);
expect(res.text).to.have.string("Built by");
});

it("handles missing files", async () => {
const res = await chai.request(testServerUrl).get("/_file/idontexist.csv");
expect(res).to.have.status(404);
expect(res.text).to.have.string("File not found");
});

it("serves exported files", async () => {
const res = await chai.request(testServerUrl).get("/robots.txt");
expect(res).to.have.status(200);
expect(res.text).to.have.string("User-agent:");
});

it("serves exported files built with a data loader", async () => {
const res = await chai.request(testServerUrl).get("/asset.txt");
expect(res).to.have.status(200);
expect(res.text).to.have.string("Built by");
});
});