Skip to content

add refetch API #92

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
merged 1 commit into from
Dec 19, 2022
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
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ export default class IncludeFragmentElement extends HTMLElement {
return fetch(request)
}

refetch() {
privateData.delete(this)
this.#handleData()
}

#observer = new IntersectionObserver(
entries => {
for (const entry of entries) {
Expand Down Expand Up @@ -169,11 +174,17 @@ export default class IncludeFragmentElement extends HTMLElement {
const canceled = !this.dispatchEvent(
new CustomEvent('include-fragment-replace', {cancelable: true, detail: {fragment}})
)
if (canceled) return
if (canceled) {
this.#busy = false
return
}

this.replaceWith(fragment)
this.dispatchEvent(new CustomEvent('include-fragment-replaced'))
} catch {
this.classList.add('is-error')
} finally {
this.#busy = false
}
}

Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ suite('include-fragment-element', function () {
teardown(() => {
document.body.innerHTML = ''
})

test('create from document.createElement', function () {
const el = document.createElement('include-fragment')
assert.equal('INCLUDE-FRAGMENT', el.nodeName)
Expand Down Expand Up @@ -141,6 +142,19 @@ suite('include-fragment-element', function () {
)
})

test('skips cache when using refetch', async function () {
const el = document.createElement('include-fragment')
el.src = '/count'

let data = await el.data
assert.equal('1', data)

el.refetch()

data = await el.data
assert.equal('2', data)
})

test('data with src attribute', function () {
const el = document.createElement('include-fragment')
el.setAttribute('src', '/hello')
Expand Down