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:
Endilie Yacop Sucipto 2019-04-21 16:54:14 +07:00 committed by GitHub
parent 2191fd662c
commit 90a1fbc535
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 132 additions and 30 deletions

View file

@ -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). // Client-side render (e.g: running in browser) to become single-page application (SPA).
if (typeof window !== 'undefined' && typeof document !== 'undefined') { 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 production, attempt to hydrate existing markup for performant first-load experience.
// For development, there is no existing markup so we had to render it. // 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. // Note that we also preload async component to avoid first-load loading screen.

View file

@ -9,32 +9,10 @@ import routesChunkNames from '@generated/routesChunkNames';
import routes from '@generated/routes'; // eslint-disable-line import routes from '@generated/routes'; // eslint-disable-line
import prefetchHelper from './prefetch'; import prefetchHelper from './prefetch';
import preloadHelper from './preload'; import preloadHelper from './preload';
import flat from './flat';
const fetched = {}; 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 => { const canPrefetchOrLoad = routePath => {
// if user is on slow or constrained connection // if user is on slow or constrained connection
if (`connection` in navigator) { if (`connection` in navigator) {
@ -58,7 +36,7 @@ const docusaurus = {
const matches = matchRoutes(routes, routePath); const matches = matchRoutes(routes, routePath);
const chunkNamesNeeded = matches.reduce((arr, match) => { const chunkNamesNeeded = matches.reduce((arr, match) => {
const chunkNames = Object.values( const chunkNames = Object.values(
flatten(routesChunkNames[match.route.path]), flat(routesChunkNames[match.route.path]),
); );
return arr.concat(chunkNames); return arr.concat(chunkNames);
}, []); }, []);

View file

@ -42,7 +42,7 @@ function Link(props) {
if (IOSupported && ref && isInternal) { if (IOSupported && ref && isInternal) {
// If IO supported and element reference found, setup Observer functionality // If IO supported and element reference found, setup Observer functionality
handleIntersection(ref, () => { handleIntersection(ref, () => {
window.__docusaurus.prefetch(targetLink); window.docusaurus.prefetch(targetLink);
}); });
} }
}; };
@ -50,7 +50,7 @@ function Link(props) {
useEffect(() => { useEffect(() => {
// If IO is not supported. We prefetch by default (only once) // If IO is not supported. We prefetch by default (only once)
if (!IOSupported && isInternal) { if (!IOSupported && isInternal) {
window.__docusaurus.prefetch(targetLink); window.docusaurus.prefetch(targetLink);
} }
// when unmount, stops intersection observer from watching // when unmount, stops intersection observer from watching
return () => { return () => {
@ -66,7 +66,7 @@ function Link(props) {
) : ( ) : (
<Perimeter <Perimeter
padding={preloadProximity} padding={preloadProximity}
onBreach={() => window.__docusaurus.preload(targetLink)} onBreach={() => window.docusaurus.preload(targetLink)}
once> once>
<NavLink {...props} innerRef={handleRef} to={targetLink} /> <NavLink {...props} innerRef={handleRef} to={targetLink} />
</Perimeter> </Perimeter>

View 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;

View 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',
});
});
});

View file

@ -68,8 +68,8 @@ function Home() {
useEffect(() => { useEffect(() => {
// Prefetch feedback pages & getting started pages // Prefetch feedback pages & getting started pages
__docusaurus.prefetch(feedbackUrl); window.docusaurus.prefetch(feedbackUrl);
__docusaurus.prefetch(gettingStartedUrl); window.docusaurus.prefetch(gettingStartedUrl);
}, []); }, []);
return ( return (