-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Patterns deployment with hashes #1089
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,17 @@ server { | |
return 200 'Success!'; | ||
} | ||
|
||
# JS and CSS files can be saved for a year, as they include a hash. | ||
location ~* \.(?:css|js)$ { | ||
expires 1y; | ||
add_header Cache-Control "public"; | ||
} | ||
# TODO Decide on a sensible cache period for images | ||
location ~* \.(?:jpg|gif|png|ico|svg)$ { | ||
expires 1d; | ||
add_header Cache-Control "public"; | ||
} | ||
|
||
# When requesting static paths with an extension, try them, and return a 404 if not found | ||
location ~* .+\..+ { | ||
try_files $uri $uri/ =404; | ||
|
@@ -18,6 +29,8 @@ server { | |
# When requesting a path without extension, try it, and return the index if not found | ||
# This allows for client-side route handling | ||
location / { | ||
try_files $uri $uri/ /index.html$is_args$args; | ||
# The index file references CSS and JS bundles, so we need to make sure it's always up to date | ||
expires -1; | ||
try_files $uri $uri/ /index.html; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have removed |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { generateManifest } from '@hypothesis/frontend-build'; | ||
import fs from 'fs'; | ||
|
||
const templateMap = [ | ||
['{{ css_bundle }}', 'styles/pattern-library.css'], | ||
['{{ js_bundle }}', 'scripts/pattern-library.bundle.js'], | ||
]; | ||
|
||
export async function buildPatternLibraryIndex() { | ||
const manifest = await generateManifest({ | ||
pattern: 'build/{scripts,styles}/pattern-library*.{css,js}', | ||
}); | ||
const templateContent = fs | ||
.readFileSync('templates/index.template.html') | ||
.toString(); | ||
|
||
const processedTemplateContent = templateMap.reduce( | ||
(content, [pattern, fileName]) => { | ||
return content.replace(pattern, manifest[fileName] ?? pattern); | ||
}, | ||
templateContent | ||
); | ||
|
||
fs.writeFileSync('build/index.html', processedTemplateContent); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered introducing mustache back, but the use case is so simple that it felt unnecessary, but I'm open to do it. No strong opinion here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The two most important things IMO are:
I think we can get away with fake templates for now, although it does mean that someone will have to know to change this if they decide to add other dynamic content in future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The main challenge here is mapping the manifest entries with their corresponding template variable, in a way that we don't couple the logic with the contents of the manifest itself (as it is above) However, this is true even if we use a proper templating system. For example, with mustache this could look something like this: const templateMap = [
['css_bundle', 'styles/pattern-library.css'],
['js_bundle', 'scripts/pattern-library.bundle.js'],
];
const manifest = await generateManifest({
pattern: 'build/{scripts,styles}/pattern-library*.{css,js}',
});
const templateContent = fs
.readFileSync('templates/index.template.html')
.toString();
const viewParams = templateMap.reduce(
(view, [pattern, fileName]) => {
view[pattern] = manifest[fileName] ?? pattern
return view;
},
{}
);
const processedTemplateContent = Mustache.render(templateContent, viewParams);
fs.writeFileSync('build/index.html', processedTemplateContent); The obvious benefit of a templating system is that the replacement of variables is less fragile (extra/missing spaces and such). |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,10 @@ | |
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>UI component pattern library</title> | ||
<link rel="stylesheet" href="/styles/pattern-library.css"> | ||
<link rel="stylesheet" href="/{{ css_bundle }}"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest the whole path should be a variable, including the leading |
||
</head> | ||
<body> | ||
<div id="app"></div> | ||
<script type="module" src="/scripts/pattern-library.bundle.js"></script> | ||
<script type="module" src="/{{ js_bundle }}"></script> | ||
</body> | ||
</html> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is no longer needed, because the index file is now built and placed in the build directory, so the step above includes it.