mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-21 04:57:05 +02:00
fix(v2): flat function & window.docusaurus rename (#1377)
* fix(v2): flat function & window.docusaurus rename * test nits * nits space * test fix
This commit is contained in:
parent
2191fd662c
commit
90a1fbc535
6 changed files with 132 additions and 30 deletions
|
@ -16,7 +16,7 @@ import routes from '@generated/routes'; // eslint-disable-line
|
|||
|
||||
// Client-side render (e.g: running in browser) to become single-page application (SPA).
|
||||
if (typeof window !== 'undefined' && typeof document !== 'undefined') {
|
||||
window.__docusaurus = docusaurus;
|
||||
window.docusaurus = docusaurus;
|
||||
// For production, attempt to hydrate existing markup for performant first-load experience.
|
||||
// For development, there is no existing markup so we had to render it.
|
||||
// Note that we also preload async component to avoid first-load loading screen.
|
||||
|
|
|
@ -9,32 +9,10 @@ import routesChunkNames from '@generated/routesChunkNames';
|
|||
import routes from '@generated/routes'; // eslint-disable-line
|
||||
import prefetchHelper from './prefetch';
|
||||
import preloadHelper from './preload';
|
||||
import flat from './flat';
|
||||
|
||||
const fetched = {};
|
||||
|
||||
function flatten(target) {
|
||||
const delimiter = '.';
|
||||
const output = {};
|
||||
|
||||
function step(object, prev) {
|
||||
Object.keys(object).forEach(key => {
|
||||
const value = object[key];
|
||||
const isArray = Array.isArray(value);
|
||||
const type = typeof value;
|
||||
const isObject = type === 'object' && !!value;
|
||||
const newKey = prev ? prev + delimiter + key : key;
|
||||
|
||||
if (!isArray && isObject && Object.keys(value).length) {
|
||||
step(value, newKey);
|
||||
return;
|
||||
}
|
||||
output[newKey] = value;
|
||||
});
|
||||
}
|
||||
step(target);
|
||||
return output;
|
||||
}
|
||||
|
||||
const canPrefetchOrLoad = routePath => {
|
||||
// if user is on slow or constrained connection
|
||||
if (`connection` in navigator) {
|
||||
|
@ -58,7 +36,7 @@ const docusaurus = {
|
|||
const matches = matchRoutes(routes, routePath);
|
||||
const chunkNamesNeeded = matches.reduce((arr, match) => {
|
||||
const chunkNames = Object.values(
|
||||
flatten(routesChunkNames[match.route.path]),
|
||||
flat(routesChunkNames[match.route.path]),
|
||||
);
|
||||
return arr.concat(chunkNames);
|
||||
}, []);
|
||||
|
|
|
@ -42,7 +42,7 @@ function Link(props) {
|
|||
if (IOSupported && ref && isInternal) {
|
||||
// If IO supported and element reference found, setup Observer functionality
|
||||
handleIntersection(ref, () => {
|
||||
window.__docusaurus.prefetch(targetLink);
|
||||
window.docusaurus.prefetch(targetLink);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -50,7 +50,7 @@ function Link(props) {
|
|||
useEffect(() => {
|
||||
// If IO is not supported. We prefetch by default (only once)
|
||||
if (!IOSupported && isInternal) {
|
||||
window.__docusaurus.prefetch(targetLink);
|
||||
window.docusaurus.prefetch(targetLink);
|
||||
}
|
||||
// when unmount, stops intersection observer from watching
|
||||
return () => {
|
||||
|
@ -66,7 +66,7 @@ function Link(props) {
|
|||
) : (
|
||||
<Perimeter
|
||||
padding={preloadProximity}
|
||||
onBreach={() => window.__docusaurus.preload(targetLink)}
|
||||
onBreach={() => window.docusaurus.preload(targetLink)}
|
||||
once>
|
||||
<NavLink {...props} innerRef={handleRef} to={targetLink} />
|
||||
</Perimeter>
|
||||
|
|
30
packages/docusaurus/lib/client/flat.js
Normal file
30
packages/docusaurus/lib/client/flat.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
function flat(target) {
|
||||
const delimiter = '.';
|
||||
const output = {};
|
||||
|
||||
function step(object, prev) {
|
||||
Object.keys(object).forEach(key => {
|
||||
const value = object[key];
|
||||
const type = typeof value;
|
||||
const isObject = type === 'object' && !!value;
|
||||
const newKey = prev ? prev + delimiter + key : key;
|
||||
|
||||
if (isObject && Object.keys(value).length) {
|
||||
step(value, newKey);
|
||||
return;
|
||||
}
|
||||
output[newKey] = value;
|
||||
});
|
||||
}
|
||||
step(target);
|
||||
return output;
|
||||
}
|
||||
|
||||
export default flat;
|
94
packages/docusaurus/test/client/flat.test.js
Normal file
94
packages/docusaurus/test/client/flat.test.js
Normal file
|
@ -0,0 +1,94 @@
|
|||
/**
|
||||
* Copyright (c) 2017-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import flat from '@lib/client/flat';
|
||||
|
||||
describe('flat', () => {
|
||||
test('nested', () => {
|
||||
expect(
|
||||
flat({
|
||||
foo: {
|
||||
bar: {
|
||||
baz: 'lorem ipsum',
|
||||
},
|
||||
},
|
||||
}),
|
||||
).toEqual({
|
||||
'foo.bar.baz': 'lorem ipsum',
|
||||
});
|
||||
|
||||
expect(
|
||||
flat({
|
||||
foo: {
|
||||
bar: 'baz',
|
||||
},
|
||||
}),
|
||||
).toEqual({
|
||||
'foo.bar': 'baz',
|
||||
});
|
||||
});
|
||||
|
||||
test('primitives', () => {
|
||||
const primitives = {
|
||||
String: 'good morning',
|
||||
Number: 1234.99,
|
||||
Boolean: true,
|
||||
Date: new Date(),
|
||||
null: null,
|
||||
undefined,
|
||||
};
|
||||
Object.keys(primitives).forEach(key => {
|
||||
const value = primitives[key];
|
||||
expect(
|
||||
flat({
|
||||
foo: {
|
||||
bar: value,
|
||||
},
|
||||
}),
|
||||
).toEqual({
|
||||
'foo.bar': value,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
test('multiple keys', () => {
|
||||
expect(
|
||||
flat({
|
||||
foo: {
|
||||
bar: 'baz',
|
||||
endi: 'lie',
|
||||
},
|
||||
}),
|
||||
).toEqual({
|
||||
'foo.bar': 'baz',
|
||||
'foo.endi': 'lie',
|
||||
});
|
||||
});
|
||||
|
||||
test('empty object', () => {
|
||||
expect(
|
||||
flat({
|
||||
foo: {
|
||||
bar: {},
|
||||
},
|
||||
}),
|
||||
).toEqual({
|
||||
'foo.bar': {},
|
||||
});
|
||||
});
|
||||
|
||||
test('array', () => {
|
||||
expect(
|
||||
flat({
|
||||
hello: [{world: {again: 'foo'}}, {lorem: 'ipsum'}],
|
||||
}),
|
||||
).toEqual({
|
||||
'hello.0.world.again': 'foo',
|
||||
'hello.1.lorem': 'ipsum',
|
||||
});
|
||||
});
|
||||
});
|
|
@ -68,8 +68,8 @@ function Home() {
|
|||
|
||||
useEffect(() => {
|
||||
// Prefetch feedback pages & getting started pages
|
||||
__docusaurus.prefetch(feedbackUrl);
|
||||
__docusaurus.prefetch(gettingStartedUrl);
|
||||
window.docusaurus.prefetch(feedbackUrl);
|
||||
window.docusaurus.prefetch(gettingStartedUrl);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue