-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
40 lines (35 loc) · 1006 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
type ElmPagesInit = {
load: (elmLoaded: Promise<unknown>) => Promise<void>;
flags: unknown;
};
interface ElmApp {
ports: {
requestUrlChange: {
subscribe: (callback: (newQuery: string) => void) => void;
};
onUrlChange: {
send: (newUrl: string) => void;
};
};
}
const config: ElmPagesInit = {
load: async function (elmLoaded) {
const app = await elmLoaded as ElmApp;
if (app.ports.requestUrlChange) {
app.ports.requestUrlChange.subscribe((newQuery: string) => {
// Build the new href with the new query
const newHref = window.location.pathname + newQuery;
// Push it into history
window.history.replaceState({}, "", newHref);
// Notify Elm of the new URL
if (app.ports.onUrlChange) {
app.ports.onUrlChange.send(window.location.href);
}
});
}
},
flags: function () {
return "You can decode this in Shared.elm using Json.Decode.string!";
},
};
export default config;