Skip to content

Commit 311cd58

Browse files
authored
file.href (#1113)
* file.href * list name first
1 parent 263fe66 commit 311cd58

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

docs/javascript/files.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ For missing files, `file.lastModified` is undefined. The `file.mimeType` is dete
9999

100100
The contents often dictate the appropriate method — for example, an Apache Arrow file is almost always read with `file.arrow`. When multiple methods are valid, choose based on your needs. For example, you can load a CSV file using `file.text` to implement parsing yourself instead of using D3.
101101

102-
In addition to the above, you can get the resolved absolute URL of the file using `file.url`. This returns a [promise](./promises) to a string:
102+
In addition to the above, you can get the resolved absolute URL of the file using `file.href`:
103103

104104
```js echo
105-
FileAttachment("volcano.json").url()
105+
FileAttachment("volcano.json").href
106106
```
107107

108108
See [file-based routing](../routing#files) for additional details.

src/client/stdlib/fileAttachment.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
const files = new Map();
22

33
export function registerFile(name, file) {
4-
const url = new URL(name, location).href;
5-
if (file == null) files.delete(url);
6-
else files.set(url, file);
4+
const href = new URL(name, location).href;
5+
if (file == null) files.delete(href);
6+
else files.set(href, file);
77
}
88

9-
export function FileAttachment(name, base = location.href) {
9+
export function FileAttachment(name, base = location) {
1010
if (new.target !== undefined) throw new TypeError("FileAttachment is not a constructor");
11-
const url = new URL(name, base).href;
12-
const file = files.get(url);
11+
const href = new URL(name, base).href;
12+
const file = files.get(href);
1313
if (!file) throw new Error(`File not found: ${name}`);
1414
const {path, mimeType, lastModified} = file;
1515
return new FileAttachmentImpl(new URL(path, location).href, name.split("/").pop(), mimeType, lastModified);
1616
}
1717

1818
async function remote_fetch(file) {
19-
const response = await fetch(await file.url());
19+
const response = await fetch(file.href);
2020
if (!response.ok) throw new Error(`Unable to load file: ${file.name}`);
2121
return response;
2222
}
@@ -29,8 +29,8 @@ async function dsv(file, delimiter, {array = false, typed = false} = {}) {
2929

3030
export class AbstractFile {
3131
constructor(name, mimeType = "application/octet-stream", lastModified) {
32-
Object.defineProperty(this, "mimeType", {value: `${mimeType}`, enumerable: true});
3332
Object.defineProperty(this, "name", {value: `${name}`, enumerable: true});
33+
Object.defineProperty(this, "mimeType", {value: `${mimeType}`, enumerable: true});
3434
if (lastModified !== undefined) Object.defineProperty(this, "lastModified", {value: Number(lastModified), enumerable: true}); // prettier-ignore
3535
}
3636
async blob() {
@@ -57,14 +57,13 @@ export class AbstractFile {
5757
return dsv(this, "\t", options);
5858
}
5959
async image(props) {
60-
const url = await this.url();
6160
return new Promise((resolve, reject) => {
6261
const i = new Image();
63-
if (new URL(url, document.baseURI).origin !== new URL(location).origin) i.crossOrigin = "anonymous";
62+
if (new URL(this.href, document.baseURI).origin !== new URL(location).origin) i.crossOrigin = "anonymous";
6463
Object.assign(i, props);
6564
i.onload = () => resolve(i);
6665
i.onerror = () => reject(new Error(`Unable to load file: ${this.name}`));
67-
i.src = url;
66+
i.src = this.href;
6867
});
6968
}
7069
async arrow() {
@@ -96,12 +95,13 @@ export class AbstractFile {
9695
}
9796

9897
class FileAttachmentImpl extends AbstractFile {
99-
constructor(url, name, mimeType, lastModified) {
98+
constructor(href, name, mimeType, lastModified) {
10099
super(name, mimeType, lastModified);
101-
Object.defineProperty(this, "_url", {value: url});
100+
Object.defineProperty(this, "href", {value: href});
102101
}
102+
/** @deprecated Use this.href instead. */
103103
async url() {
104-
return `${await this._url}`;
104+
return this.href;
105105
}
106106
}
107107

0 commit comments

Comments
 (0)