Skip to content

Commit f954b31

Browse files
committed
Add Store.parseMetaTags
1 parent f934c18 commit f954b31

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This changelog covers all three packages, as they are (for now) updated as a whole
44

5+
## UNRELEASED
6+
7+
- Add `Store.parseMetaTags` to load JSON-AD objects stored in the DOM. Speeds up initial page load by allowing server to set JSON-AD objects in the initial HTML response.
8+
59
## v0.35.0
610

711
### @tomic/react

data-browser/src/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ const store = new Store({
4040
serverUrl,
4141
});
4242

43+
store.parseMetaTags();
44+
4345
declare global {
4446
interface Window {
4547
bugsnagApiKey: string;

lib/src/store.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
unknownSubject,
1010
urls,
1111
Commit,
12+
JSONADParser,
1213
} from './index.js';
1314
import { authenticate, fetchWebSocket, startWebsocket } from './websockets.js';
1415

@@ -412,6 +413,28 @@ export class Store {
412413
this.eventManager.emit(StoreEvents.ResourceManuallyCreated, resource);
413414
}
414415

416+
/** Parses the HTML document for `JSON-AD` data in <meta> tags, adds it to the store */
417+
public parseMetaTags(): void {
418+
const metaTags = document.querySelectorAll(
419+
'meta[property="json-ad-initial"]',
420+
);
421+
const parser = new JSONADParser();
422+
423+
metaTags.forEach(tag => {
424+
const content = tag.getAttribute('content');
425+
426+
if (content === null) {
427+
return;
428+
}
429+
430+
// convert base64 content to JSON
431+
const json = JSON.parse(atob(content));
432+
433+
const [_, resources] = parser.parseObject(json);
434+
this.addResources(...resources);
435+
});
436+
}
437+
415438
/** Removes (destroys / deletes) resource from this store */
416439
public removeResource(subject: string): void {
417440
const resource = this.resources.get(subject);

0 commit comments

Comments
 (0)