Repository navigation
Make caching on client side optional #4940
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
Merged
+224
−9
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2026 LycheeOrg. | ||
| */ | ||
|
|
||
| // @vitest-environment jsdom | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
| import axios, { type AxiosAdapter, type InternalAxiosRequestConfig } from "axios"; | ||
| import type { AxiosCacheInstance } from "axios-cache-interceptor"; | ||
| import { parseClientCacheEnabled, setUpClientCache } from "@/config/client-cache"; | ||
|
|
||
| function metaTag(content: string): Element { | ||
| const meta = document.createElement("meta"); | ||
| meta.setAttribute("name", "lychee-client-cache"); | ||
| meta.setAttribute("content", content); | ||
| return meta; | ||
| } | ||
|
|
||
| // An instance whose requests never leave the test: every one is recorded and answered with a | ||
| // response the cache would keep for an hour. | ||
| function stubbedInstance(enabled: boolean): { instance: AxiosCacheInstance; sent: InternalAxiosRequestConfig[] } { | ||
| const sent: InternalAxiosRequestConfig[] = []; | ||
| const instance = axios.create(); | ||
| instance.defaults.adapter = ((config: InternalAxiosRequestConfig) => { | ||
| sent.push(config); | ||
| return Promise.resolve({ data: "asset", status: 200, statusText: "OK", headers: { "cache-control": "private, max-age=3600" }, config }); | ||
| }) as AxiosAdapter; | ||
| setUpClientCache(instance, enabled); | ||
| return { instance: instance as unknown as AxiosCacheInstance, sent }; | ||
| } | ||
|
|
||
| describe("parseClientCacheEnabled()", () => { | ||
| it("is off when the tag says 0", () => { | ||
| expect(parseClientCacheEnabled(metaTag("0"))).toBe(false); | ||
| }); | ||
|
|
||
| it("is on when the tag says 1", () => { | ||
| expect(parseClientCacheEnabled(metaTag("1"))).toBe(true); | ||
| }); | ||
|
|
||
| it("is on without the tag", () => { | ||
| expect(parseClientCacheEnabled(null)).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("setUpClientCache()", () => { | ||
| it("answers a repeated request from its storage when on", async () => { | ||
| const { instance, sent } = stubbedInstance(true); | ||
|
|
||
| await instance.get("Asset/album/photo/small", { id: "asset" }); | ||
| const second = await instance.get("Asset/album/photo/small", { id: "asset" }); | ||
|
|
||
| expect(sent).toHaveLength(1); | ||
| expect(second.cached).toBe(true); | ||
| }); | ||
|
|
||
| it("sends every request to the network with the no-cache headers when off", async () => { | ||
| const { instance, sent } = stubbedInstance(false); | ||
|
|
||
| await instance.get("Asset/album/photo/small", { id: "asset" }); | ||
| await instance.get("Asset/album/photo/small", { id: "asset" }); | ||
|
|
||
| expect(sent).toHaveLength(2); | ||
| for (const config of sent) { | ||
| expect(config.headers.get("Cache-Control")).toBe("no-cache, no-store, must-revalidate, max-age=0"); | ||
| expect(config.headers.get("Pragma")).toBe("no-cache"); | ||
| expect(config.headers.get("Expires")).toBe("0"); | ||
| } | ||
| }); | ||
|
|
||
| it("still answers a request that opts in from its storage when off", async () => { | ||
| const { instance, sent } = stubbedInstance(false); | ||
|
|
||
| await instance.get("Asset/album/photo/small", { id: "asset", cache: { enabled: true } }); | ||
| const second = await instance.get("Asset/album/photo/small", { id: "asset", cache: { enabled: true } }); | ||
|
|
||
| expect(sent).toHaveLength(1); | ||
| expect(second.cached).toBe(true); | ||
| }); | ||
|
|
||
| it("keeps its storage when off, so cache clearing still works", () => { | ||
| const { instance } = stubbedInstance(false); | ||
|
|
||
| expect(() => instance.storage.remove("asset")).not.toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2026 LycheeOrg. | ||
| */ | ||
|
|
||
| import type { AxiosInstance, InternalAxiosRequestConfig } from "axios"; | ||
| import { setupCache } from "axios-cache-interceptor"; | ||
|
|
||
| /** | ||
| * Client-side cache switch chosen by the server. | ||
| * | ||
| * `Meta` renders `<meta name="lychee-client-cache" content="0|1">` from `CLIENT_CACHE_ENABLED`, | ||
| * so the value is known before the first request. The tag is read once and cached; only | ||
| * `content="0"` turns the cache off, a missing tag means on (the flag's default). | ||
| */ | ||
| let cached: boolean | undefined = undefined; | ||
|
|
||
| export function parseClientCacheEnabled(meta: Element | null): boolean { | ||
| return meta?.getAttribute("content") !== "0"; | ||
| } | ||
|
|
||
| export function isClientCacheEnabled(): boolean { | ||
| cached ??= parseClientCacheEnabled(document.querySelector('meta[name="lychee-client-cache"]')); | ||
| return cached; | ||
| } | ||
|
|
||
| // The headers axios-cache-interceptor's `cacheTakeover` adds to every request when enabled: they | ||
| // keep the browser HTTP cache from answering, which a disabled interceptor no longer does. | ||
| function addNoCacheHeaders(config: InternalAxiosRequestConfig): InternalAxiosRequestConfig { | ||
| config.headers.set("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0", false); | ||
| config.headers.set("Pragma", "no-cache", false); | ||
| config.headers.set("Expires", "0", false); | ||
| return config; | ||
| } | ||
|
|
||
| /** | ||
| * Installs the response cache on `instance`. Disabled, it stores and serves nothing but keeps its | ||
| * storage, so `clearCache()` callers still work (they just remove nothing). | ||
| */ | ||
| export function setUpClientCache(instance: AxiosInstance, enabled: boolean): void { | ||
| setupCache(instance, { enabled: enabled }); | ||
| if (!enabled) { | ||
| instance.interceptors.request.use(addNoCacheHeaders); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-License-Identifier: MIT | ||
| * Copyright (c) 2017-2018 Tobias Reich | ||
| * Copyright (c) 2018-2026 LycheeOrg. | ||
| */ | ||
|
|
||
| /** | ||
| * We don't care for unhandled exceptions in tests. | ||
| * It is the nature of a test to throw an exception. | ||
| * Without this suppression we had 100+ Linter warning in this file which | ||
| * don't help anything. | ||
| * | ||
| * @noinspection PhpDocMissingThrowsInspection | ||
| * @noinspection PhpUnhandledExceptionInspection | ||
| */ | ||
|
|
||
| namespace Tests\Feature_v3\ClientCache; | ||
|
|
||
| use Tests\Feature_v3\Base\BaseApiWithDataTest; | ||
|
|
||
| /** | ||
| * The client cache flag is rendered into the HTML page. | ||
| */ | ||
| class ClientCacheMetaTest extends BaseApiWithDataTest | ||
| { | ||
| public function tearDown(): void | ||
| { | ||
| config(['features.client-cache' => true]); | ||
| parent::tearDown(); | ||
| } | ||
|
|
||
| public function testEnabledByDefault(): void | ||
| { | ||
| $this->assertMetaTag('1'); | ||
|
ildyria marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| public function testDisabled(): void | ||
| { | ||
| config(['features.client-cache' => false]); | ||
|
|
||
| $this->assertMetaTag('0'); | ||
| } | ||
|
|
||
| private function assertMetaTag(string $content): void | ||
| { | ||
| $response = $this->get('/gallery'); | ||
|
|
||
| $this->assertOk($response); | ||
| $response->assertSee('<meta name="lychee-client-cache" content="' . $content . '">', false); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.