Merge pull request #519 from outline/jori/prefetch

Added link=“prefetch” tags
This commit is contained in:
Jori Lallo
2018-01-26 11:21:34 -08:00
committed by GitHub
7 changed files with 86 additions and 25 deletions

View File

@@ -0,0 +1,35 @@
// @flow
import React from 'react';
import fs from 'fs';
import path from 'path';
import webpackConfig from '../../webpack.config';
const PUBLIC_PATH = webpackConfig.output.publicPath;
const prefetchTags = [
<link rel="dns-prefetch" href={process.env.AWS_S3_UPLOAD_BUCKET_URL} />,
];
try {
const manifest = fs.readFileSync(
path.join(__dirname, '../../dist/manifest.json'),
'utf8'
);
const manifestData = JSON.parse(manifest);
Object.values(manifestData).forEach(filename => {
if (typeof filename !== 'string') return;
if (filename.endsWith('.js')) {
prefetchTags.push(
<link rel="prefetch" href={`${PUBLIC_PATH}${filename}`} as="script" />
);
} else if (filename.endsWith('.css')) {
prefetchTags.push(
<link rel="prefetch" href={`${PUBLIC_PATH}${filename}`} as="style" />
);
}
});
} catch (_e) {
// no-op
}
export default prefetchTags;