Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/frontend/lib/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
35 changes: 18 additions & 17 deletions packages/frontend/model/clean.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import compose from 'compose-function';
import createDOMPurify from 'dompurify';
import { JSDOM } from 'jsdom';
import { minify } from 'html-minifier';
// import compose from 'compose-function';
// import createDOMPurify from 'dompurify';
// import { JSDOM } from 'jsdom';
// import { minify } from 'html-minifier';

// We don't represent lists in incopy, so things will just come across with bullet characters.
// These may also be used for emphasis, so bullet characters don't mean list.
export const bigBullets = (s: string) =>
s.replace(/•/g, '<span class="bullet">&bull;</span>');

const { window } = new JSDOM('');
const DOMPurify = createDOMPurify(window);
// const { window } = new JSDOM('');
// const DOMPurify = createDOMPurify(window);

export const clean = compose(
bigBullets,
(s: string) =>
minify(s, {
collapseWhitespace: true,
removeEmptyElements: true,
minifyCSS: true,
minifyJS: true,
}),
DOMPurify.sanitize,
);
export const clean: (s: string) => string = s => s;
// export const clean = compose(
// // bigBullets,
// // (s: string) =>
// // minify(s, {
// // collapseWhitespace: true,
// // removeEmptyElements: true,
// // minifyCSS: true,
// // minifyJS: true,
// // }),
// // DOMPurify.sanitize,
// );
4 changes: 3 additions & 1 deletion packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"polished": "^1.9.2",
"raven-js": "3.19.1",
"react": "^16.4.0",
"react-dom": "^16.4.0"
"react-dom": "^16.4.0",
"react-loadable": "^5.5.0"
},
"devDependencies": {
"@types/amphtml-validator": "^1.0.0",
Expand All @@ -46,6 +47,7 @@
"@types/raven-js": "^3.10.0",
"@types/react": "^16.4.11",
"@types/react-dom": "^16.0.7",
"@types/react-loadable": "^5.5.0",
"amphtml-validator": "^1.0.23"
}
}
36 changes: 30 additions & 6 deletions packages/frontend/web/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
init as initGa,
sendPageView as sendGaPageView,
} from '@frontend/web/browser/ga';
import { Article } from './pages/Article';
// import { Article } from './pages/Article';
import { ReportedError } from '@frontend/web/browser/reportError';

if (module.hot) {
Expand All @@ -17,7 +17,7 @@ if (module.hot) {
// Kick off the app
const go = () => {
const hydrate = () => {
const { cssIDs, data } = window.guardian.app;
// const { cssIDs, data } = window.guardian.app;

initGa();

Expand All @@ -30,11 +30,33 @@ const go = () => {
* in development mode to retain the sourceMap info. As detailed
* in the issue raised here https://github.com/emotion-js/emotion/issues/487
*/
if (process.env.NODE_ENV !== 'development') {
hydrateCSS(cssIDs);
}
// if (process.env.NODE_ENV !== 'development') {
// hydrateCSS(cssIDs);
// }
// hydrateApp(React.createElement(Article, { data }), container);
// import('@frontend/web/components/ClientComponent').then(c => {
// console.log('hello', c);
// const hello = document.getElementById('hello');
// hydrateApp(React.createElement(c.default, {}), hello);
// });
const islands = [...document.querySelectorAll('.js-island')];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's use <gu-island> it looks cooler

const parentIslands = islands.filter(
node => node.parentNode.closest('.js-island') === null,
);
// tslint:disable-next-line:no-unused-expression
parentIslands.map(island => {
console.log(island);
const path = island.dataset.island;
const d = JSON.parse(island.dataset.data);

hydrateApp(React.createElement(Article, { data }), container);
// can't really use the @frontend/web/ syntax in the browser
import('./components/' + path).then(component => {
hydrateApp(
React.createElement(component.default, d),
island,
);
});
});
}

sendGaPageView();
Expand Down Expand Up @@ -104,3 +126,5 @@ if (window.guardian.polyfilled) {
} else {
window.guardian.onPolyfilled = go;
}

console.log('HELLO IM RUNNIGN');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😹

7 changes: 7 additions & 0 deletions packages/frontend/web/components/ClientComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react';

const ClientComponent: React.FC<{ hello: string }> = ({ hello }) => (
<h1>hello frends {hello}</h1>
);
// tslint:disable-next-line: no-default-export
export default ClientComponent;
31 changes: 31 additions & 0 deletions packages/frontend/web/components/lib/Island.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// import Loadable from 'react-loadable';
import React from 'react';

interface IslandProps<P> {
component: React.ComponentType<P>;
from: string;
data: P;
}

/**
* Hydrates a component in the client by async loading the exported component.
* This does not take children.
* THE COMPONENT MUST BE A DEFAULT EXPORT FOR NOW.
* i am aware that this makes me hypocritical
*
* @param component - The component as it is exported.
* @param from - The location of the component in /components.
* @param data - What you want fed into the component.
*
*/
export const Island: <P>(
_: IslandProps<P>,
) => React.ReactElement<IslandProps<P>> = ({ component, from, data }) => (
<div
className="js-island"
data-island={from}
data-data={JSON.stringify(data)}
>
{React.createElement(component, data)}
</div>
);
7 changes: 7 additions & 0 deletions packages/frontend/web/pages/Article.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { ArticleBody } from '@frontend/web/components/ArticleBody';
import { BackToTop } from '@frontend/web/components/BackToTop';
import { SubNav } from '@frontend/web/components/Header/Nav/SubNav/SubNav';
import { CookieBanner } from '@frontend/web/components/CookieBanner';
import ClientComponent from '@frontend/web/components/ClientComponent';
import { Island } from '../components/lib/Island';

// TODO: find a better of setting opacity
const articleWrapper = css`
Expand Down Expand Up @@ -55,6 +57,11 @@ export const Article: React.FC<{
/>
<main className={articleWrapper}>
<Container className={articleContainer}>
<Island
from="ClientComponent"
component={ClientComponent}
data={{ hello: 'world' }}
/>
Comment thread
AWare marked this conversation as resolved.
<article>
<ArticleBody CAPI={data.CAPI} config={data.config} />
<div className={secondaryColumn} />
Expand Down
37 changes: 36 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,13 @@
dependencies:
"@types/react" "*"

"@types/react-loadable@^5.5.0":
version "5.5.0"
resolved "https://registry.yarnpkg.com/@types/react-loadable/-/react-loadable-5.5.0.tgz#7a878408ad940250aeb91d77d2b54a18f3c7c946"
dependencies:
"@types/react" "*"
"@types/webpack" "*"

"@types/react-test-renderer@^16.0.3":
version "16.0.3"
resolved "https://registry.yarnpkg.com/@types/react-test-renderer/-/react-test-renderer-16.0.3.tgz#cce5c983d66cc5c3582e7c2f44b274ab635a8acc"
Expand Down Expand Up @@ -995,6 +1002,16 @@
version "1.13.6"
resolved "https://registry.yarnpkg.com/@types/webpack-env/-/webpack-env-1.13.6.tgz#128d1685a7c34d31ed17010fc87d6a12c1de6976"

"@types/webpack@*":
version "4.4.25"
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-4.4.25.tgz#c8a1eb968a33a3e6da641f529c5add0d44d34809"
dependencies:
"@types/anymatch" "*"
"@types/node" "*"
"@types/tapable" "*"
"@types/uglify-js" "*"
source-map "^0.6.0"

"@types/webpack@^4.4.11":
version "4.4.22"
resolved "https://registry.yarnpkg.com/@types/webpack/-/webpack-4.4.22.tgz#c4a5ea8b74a31b579537515bcfe86d2b2a34382c"
Expand Down Expand Up @@ -5340,7 +5357,7 @@ longest-streak@^2.0.1:
version "2.0.2"
resolved "https://registry.yarnpkg.com/longest-streak/-/longest-streak-2.0.2.tgz#2421b6ba939a443bb9ffebf596585a50b4c38e2e"

loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.3.1:
loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.3.1, loose-envify@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
dependencies:
Expand Down Expand Up @@ -6860,6 +6877,14 @@ prompts@^0.1.9:
kleur "^2.0.1"
sisteransi "^0.1.1"

prop-types@^15.5.0:
version "15.7.2"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5"
dependencies:
loose-envify "^1.4.0"
object-assign "^4.1.1"
react-is "^16.8.1"

prop-types@^15.6.1, prop-types@^15.6.2:
version "15.6.2"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.6.2.tgz#05d5ca77b4453e985d60fc7ff8c859094a497102"
Expand Down Expand Up @@ -7042,6 +7067,16 @@ react-dom@^16.4.0:
prop-types "^15.6.2"
scheduler "^0.12.0"

react-is@^16.8.1:
version "16.8.3"
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.3.tgz#4ad8b029c2a718fc0cfc746c8d4e1b7221e5387d"

react-loadable@^5.5.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/react-loadable/-/react-loadable-5.5.0.tgz#582251679d3da86c32aae2c8e689c59f1196d8c4"
dependencies:
prop-types "^15.5.0"

react-testing-library@^5.2.3:
version "5.4.4"
resolved "https://registry.yarnpkg.com/react-testing-library/-/react-testing-library-5.4.4.tgz#3fa787999492be94b228e4540a7211556bf4fd94"
Expand Down