Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,11 @@ We follow the principle from the course: measure, set a budget and show the numb
- The Apollo cache, so we do not fetch the same data twice. Going back to a wine the user has already seen sends no requests at all.
- Debounce on the search field (300 ms). An end-to-end test checks that a whole word sends only one search.
- Apache compresses HTML, CSS and JavaScript with gzip. The main JavaScript file goes from 435 kB to 136 kB.
- Apache sends cache headers for the built files. The JavaScript and CSS files have a hash of their content in the name, so the browser keeps them for a year and does not ask for them again (`Cache-Control: public, max-age=31536000, immutable`). `index.html` gets `no-cache`: the browser asks on every visit if it has changed, and gets a short «not changed» answer when it has not. Before, Apache sent only an `ETag`, so the browser had to guess how long to keep each file or ask again for every one. The [VM guide](docs/vm.md#cache-headers) has the configuration.
- The main area of every page is at least one screen tall, so the footer does not jump while a page's code loads (see the layout shift below).

**Planned:**
- Compress the API's JSON responses in Apache. Today they are sent uncompressed.
- Cache headers in Apache for the built files. Their names contain a hash of the content, so they can be cached for a long time. Today Apache sends only an `ETag`, so the browser has to guess how long to keep them or ask again.

### Budget and measurements

Expand Down
40 changes: 39 additions & 1 deletion docs/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,55 @@ ProxyPassReverse "/project2/graphql" "http://127.0.0.1:3001/graphql"
<Directory /var/www/html/project2>
FallbackResource /project2/index.html
Header set Cache-Control "no-cache"
</Directory>
<Directory /var/www/html/project2/assets>
FallbackResource disabled
Header set Cache-Control "public, max-age=31536000, immutable"
</Directory>
```

Enable the configuration and check its syntax:

```bash
sudo a2enmod proxy proxy_http
sudo a2enmod proxy proxy_http headers
sudo a2enconf project2
sudo apache2ctl configtest
```

### Cache headers

The two `Header` lines tell the browser what it can keep between visits:

- **Files in `assets/`** have a hash of their content in the name, such as `index-BL5R2qxO.js`. A
changed file gets a new name, so the browser can keep each one for a year (`max-age=31536000`)
and never ask again (`immutable`). A returning visitor then downloads no JavaScript or CSS.
- **`index.html`** always has the same name, and it names the hashed files. `no-cache` lets the
browser store it, but makes it ask Apache on every visit if it has changed. Without this, a
visitor could keep an old `index.html` after an update and ask for files that are gone.

`FallbackResource disabled` makes a missing file in `assets/` give `404`. Without it, Apache would
answer with `index.html` and let the browser keep that answer for a year under the missing file's name.

The headers do not apply to `/project2/graphql`, because Apache forwards those requests and reads
no file from the directories.

After a reload, check the headers. Take a file name from the first command's output for the second:

```bash
curl -s http://localhost/project2/ | grep -o 'assets/index-[^"]*\.js'
curl -sI http://localhost/project2/assets/<file>.js | grep -i cache-control
curl -sI http://localhost/project2/ | grep -i cache-control
curl -sI http://localhost/project2/sok | grep -i cache-control
curl -sI http://localhost/project2/assets/missing.js | head -1
```

The first `Cache-Control` should be `public, max-age=31536000, immutable`, the next two `no-cache`,
and the last line `HTTP/1.1 404 Not Found`. See Apache's
[mod_headers documentation](https://httpd.apache.org/docs/2.4/mod/mod_headers.html#header) and MDN on
[Cache-Control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Cache-Control).

If the check reports `Syntax OK`, reload Apache and try a query:

```bash
Expand Down
2 changes: 2 additions & 0 deletions frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
name="description"
content="Kork &amp; Krone er et studentdrevet vinregister. Søk blant over 100 000 viner, se hva medstudentene dine mener, og gi din egen dom."
/>
<!-- Without this link the browser asks for /favicon.ico at the server root, which gives 404. -->
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<title>Kork &amp; Krone – vin anmeldt av studenter</title>
</head>
<body>
Expand Down
12 changes: 12 additions & 0 deletions frontend/public/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.