-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce client-side Turbo Cache Control API (#632)
* Introduce client-side Turbo Cache Control API This commit introduces a `Cache` class which exposes an API that can be used to control the value of the `turbo-cache-control` meta element form the client-side. Most of the times you just need to the set the value of the `turbo-cache-control` meta element from the server-side when you are responding to your regular HTML request. But there are some cases where you want to control the cache control behavior from the client-side after a certain action happened on the client (i.e. if you don't want to show remaining artefacts of an finsihed animation). You can achieve the same behavior by dynamically prepending a `<meta>` element to the `<head>` via JavaScript, like: ```javascript document.head.insertAdjacentHTML( 'beforeend', '<meta name="turbo-cache-control" content="no-cache">' ) ``` Though, this feels very error-prone and doesn't account for duplicate `<meta>` elements in the `<head>`. This Pull Request aims to simplify this by exposing a client-side API to control the value of the meta element. The name of the exposed functions are kept in-line with the helpers in: https://github.com/hotwired/turbo-rails/blob/43bf84a9b1d2cc78da6810b82f92be2c32c9dbfd/app/helpers/turbo/drive_helper.rb#L15 * Refactor to make use of util `getMetaContent()` throughout the codebase
- Loading branch information
Showing
6 changed files
with
62 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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,30 @@ | ||
import { Session } from "./session" | ||
import { setMetaContent } from "../util" | ||
|
||
export class Cache { | ||
readonly session: Session | ||
|
||
constructor(session: Session) { | ||
this.session = session | ||
} | ||
|
||
clear() { | ||
this.session.clearCache() | ||
} | ||
|
||
resetCacheControl() { | ||
this.setCacheControl("") | ||
} | ||
|
||
exemptPageFromCache() { | ||
this.setCacheControl("no-cache") | ||
} | ||
|
||
exemptPageFromPreview() { | ||
this.setCacheControl("no-preview") | ||
} | ||
|
||
private setCacheControl(value: string) { | ||
setMetaContent("turbo-cache-control", value) | ||
} | ||
} |
This file contains 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 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 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 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 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