-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from navikt/async-api
feat: 🎸 endre async-api til å bruke react's suspend/lazy
- Loading branch information
Showing
9 changed files
with
105 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React, {ReactNode} from "react"; | ||
import loadjs from 'loadjs'; | ||
import {createAssetManifestParser, joinPaths} from "./utils"; | ||
import {importer as importerSync} from '../navspa' | ||
|
||
|
||
const ASSET_MANIFEST_NAME = 'asset-manifest.json'; | ||
export type ManifestObject = { [k: string]: any }; | ||
export type AssetManifestParser = (manifestObject: ManifestObject) => string[]; | ||
|
||
export interface PreloadConfig { | ||
appName: string; | ||
appBaseUrl: string; | ||
assetManifestParser?: AssetManifestParser; | ||
} | ||
|
||
export interface AsyncSpaConfig extends PreloadConfig { | ||
wrapperClassName?: string; | ||
loader?: NonNullable<ReactNode>; | ||
} | ||
|
||
function createLoadJsBundleId(appName: string): string { | ||
return `async_navspa_${appName}`; | ||
} | ||
|
||
function fetchAssetUrls(appBaseUrl: string, assetManifestParser: AssetManifestParser): Promise<string[]> { | ||
return fetch(joinPaths(appBaseUrl, ASSET_MANIFEST_NAME)) | ||
.then(res => res.json()) | ||
.then(manifest => assetManifestParser(manifest)); | ||
} | ||
|
||
async function loadAssets(config: PreloadConfig): Promise<void> { | ||
const loadJsBundleId = createLoadJsBundleId(config.appName); | ||
const assetManifestParser = config.assetManifestParser || createAssetManifestParser(config.appBaseUrl); | ||
|
||
if (!loadjs.isDefined(loadJsBundleId)) { | ||
const assets: string[] = await fetchAssetUrls(config.appBaseUrl, assetManifestParser) | ||
if (!loadjs.isDefined(loadJsBundleId)) { | ||
await loadjs(assets, loadJsBundleId, {returnPromise: true}) | ||
} | ||
} | ||
} | ||
|
||
export function preload(config: PreloadConfig) { | ||
loadAssets(config) | ||
.catch(console.error); | ||
} | ||
|
||
export function importerLazy<P>(config: AsyncSpaConfig): Promise<{ default: React.ComponentType<P> }> { | ||
return loadAssets(config) | ||
.catch(console.error) | ||
.then(() => ({ default: importerSync<P>(config.appName, config.wrapperClassName) })); | ||
} | ||
|
||
export function importer<P>(config: AsyncSpaConfig): React.ComponentType<P> { | ||
const LazyComponent = React.lazy(() => importerLazy(config)); | ||
const loader = config.loader || <></>; | ||
return (props: P) => ( | ||
<React.Suspense fallback={loader}> | ||
<LazyComponent {...props} /> | ||
</React.Suspense> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,16 @@ | ||
import { importer, eksporter } from './navspa'; | ||
import { importerAsync, preloadAsync } from './async-navspa'; | ||
import { importer as importerAsync, importerLazy, preload } from './async/async-navspa'; | ||
export { AsyncSpaConfig } from './async/async-navspa'; | ||
|
||
const NAVSPA = { | ||
importer, | ||
importerAsync, | ||
preloadAsync, | ||
eksporter, | ||
export const AsyncNavspa = { | ||
importer: importerAsync, | ||
importerLazy, | ||
preload | ||
}; | ||
|
||
export default NAVSPA; | ||
export const Navspa = { | ||
importer, | ||
eksporter | ||
}; | ||
|
||
export default Navspa; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters