-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deprecate [data-turbo-cache=false] in favor of [data-turbo-temporary] (…
…#871) Renames the `[data-turbo-cache=false]` attribute (used to denote temporary elements that should be removed before caching) to `[data-turbo-temporary]` for better similarity with `[data-turbo-permanent]`, its conceptual opposite. This is a superficial change, but worth it in terms of cohesion, I think. The pairing of "temporary" with "permanent" is just too good to ignore. Also, given the existence of `turbo-cache-*` as the namespace for page-level cache control, a unique name is less likely to confuse. References: - #238
- Loading branch information
1 parent
3351d38
commit e013072
Showing
3 changed files
with
43 additions
and
14 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 |
---|---|---|
@@ -1,27 +1,44 @@ | ||
import { TurboBeforeCacheEvent } from "../core/session" | ||
|
||
export class CacheObserver { | ||
readonly selector: string = "[data-turbo-temporary]" | ||
readonly deprecatedSelector: string = "[data-turbo-cache=false]" | ||
|
||
started = false | ||
|
||
start() { | ||
if (!this.started) { | ||
this.started = true | ||
addEventListener("turbo:before-cache", this.removeStaleElements, false) | ||
addEventListener("turbo:before-cache", this.removeTemporaryElements, false) | ||
} | ||
} | ||
|
||
stop() { | ||
if (this.started) { | ||
this.started = false | ||
removeEventListener("turbo:before-cache", this.removeStaleElements, false) | ||
removeEventListener("turbo:before-cache", this.removeTemporaryElements, false) | ||
} | ||
} | ||
|
||
removeStaleElements = <EventListener>((_event: TurboBeforeCacheEvent) => { | ||
const staleElements = [...document.querySelectorAll('[data-turbo-cache="false"]')] | ||
|
||
for (const element of staleElements) { | ||
removeTemporaryElements = <EventListener>((_event: TurboBeforeCacheEvent) => { | ||
for (const element of this.temporaryElements) { | ||
element.remove() | ||
} | ||
}) | ||
|
||
get temporaryElements() { | ||
return [...document.querySelectorAll(this.selector), ...this.temporaryElementsWithDeprecation] | ||
} | ||
|
||
get temporaryElementsWithDeprecation() { | ||
const elements = document.querySelectorAll(this.deprecatedSelector) | ||
|
||
if (elements.length) { | ||
console.warn( | ||
`The ${this.deprecatedSelector} selector is deprecated and will be removed in a future version. Use ${this.selector} instead.` | ||
) | ||
} | ||
|
||
return [...elements] | ||
} | ||
} |
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