Skip to content
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
4 changes: 4 additions & 0 deletions src/runtime/api/filesystem_router.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export default [
getter: "getStyle",
cache: true,
},
assetPrefix: {
getter: "getAssetPrefix",
cache: true,
},
},
klass: {},
}),
Expand Down
11 changes: 7 additions & 4 deletions src/runtime/api/filesystem_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,11 +698,14 @@ impl FileSystemRouter {

#[bun_jsc::host_fn(getter)]
pub fn get_asset_prefix(this: &Self, global_this: &JSGlobalObject) -> JsResult<JSValue> {
if let Some(ref asset_prefix) = this.asset_prefix {
return Ok(zs_to_js(asset_prefix.leak(), global_this));
}
// An omitted `assetPrefix` and `assetPrefix: ""` both store no prefix, so report
// the empty string the router actually applies rather than null.
let prefix = match this.asset_prefix {
Some(ref asset_prefix) => asset_prefix.leak(),
None => b"",
};

Ok(JSValue::NULL)
Ok(zs_to_js(prefix, global_this))
}

// Codegen's `host_fn_finalize` calls this via `|b| FileSystemRouter::finalize(b)`
Expand Down
34 changes: 34 additions & 0 deletions test/js/bun/util/filesystem_router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,40 @@ it("assetPrefix, src, and origin", async () => {
}
});

it("assetPrefix is readable from the router", () => {
const { dir } = make(["index.tsx"]);

const router = new Bun.FileSystemRouter({
dir,
style: "nextjs",
assetPrefix: "/_next/static/",
origin: "https://nextjs.org",
});

const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(router), "assetPrefix");
expect(typeof descriptor?.get).toBe("function");
expect(descriptor?.set).toBeUndefined();
expect(Object.hasOwn(router, "assetPrefix")).toBe(false);

expect(router.assetPrefix).toBe("/_next/static/");

// the prefix the router reports is the one it applies to `src`
expect(router.match("/")!.src).toBe(`${router.origin}${router.assetPrefix}index.tsx`);

router.reload();
expect(router.assetPrefix).toBe("/_next/static/");
});

it("assetPrefix is an empty string when unset", () => {
const { dir } = make(["index.tsx"]);

for (const assetPrefix of [undefined, ""]) {
const router = new Bun.FileSystemRouter({ dir, style: "nextjs", assetPrefix });
expect(router.assetPrefix).toBe("");
expect(router.match("/")!.src).toBe("index.tsx");
}
});

it(".query works", () => {
// set up the test
const { dir } = make(["posts.tsx"]);
Expand Down
Loading