Skip to content

[LiveComponent] Fix live component rendering when loaded from bfcache #436

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
Oct 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
12 changes: 4 additions & 8 deletions src/LiveComponent/assets/dist/live_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1238,17 +1238,13 @@ class default_1 extends Controller {
this.isRerenderRequested = false;
this.requestDebounceTimeout = null;
this.pollingIntervals = [];
this.isWindowUnloaded = false;
this.isConnected = false;
this.originalDataJSON = '{}';
this.mutationObserver = null;
this.childComponentControllers = [];
this.pendingActionTriggerModelElement = null;
this.markAsWindowUnloaded = () => {
this.isWindowUnloaded = true;
};
}
initialize() {
this.markAsWindowUnloaded = this.markAsWindowUnloaded.bind(this);
this.handleUpdateModelEvent = this.handleUpdateModelEvent.bind(this);
this.handleInputEvent = this.handleInputEvent.bind(this);
this.handleChangeEvent = this.handleChangeEvent.bind(this);
Expand All @@ -1261,12 +1257,12 @@ class default_1 extends Controller {
this.synchronizeValueOfModelFields();
}
connect() {
this.isConnected = true;
this._onLoadingFinish();
if (!(this.element instanceof HTMLElement)) {
throw new Error('Invalid Element Type');
}
this._initiatePolling();
window.addEventListener('beforeunload', this.markAsWindowUnloaded);
this._startAttributesMutationObserver();
this.element.addEventListener('live:update-model', this.handleUpdateModelEvent);
this.element.addEventListener('input', this.handleInputEvent);
Expand All @@ -1277,7 +1273,6 @@ class default_1 extends Controller {
disconnect() {
this._stopAllPolling();
__classPrivateFieldGet(this, _instances, "m", _clearRequestDebounceTimeout).call(this);
window.removeEventListener('beforeunload', this.markAsWindowUnloaded);
this.element.removeEventListener('live:update-model', this.handleUpdateModelEvent);
this.element.removeEventListener('input', this.handleInputEvent);
this.element.removeEventListener('change', this.handleChangeEvent);
Expand All @@ -1287,6 +1282,7 @@ class default_1 extends Controller {
if (this.mutationObserver) {
this.mutationObserver.disconnect();
}
this.isConnected = false;
}
update(event) {
if (event.type === 'input' || event.type === 'change') {
Expand Down Expand Up @@ -1944,7 +1940,7 @@ _instances = new WeakSet(), _startPendingRequest = function _startPendingRequest
__classPrivateFieldGet(this, _instances, "m", _startPendingRequest).call(this);
});
}, _processRerender = function _processRerender(html, response) {
if (this.isWindowUnloaded) {
if (!this.isConnected) {
return;
}
if (response.headers.get('Location')) {
Expand Down
14 changes: 5 additions & 9 deletions src/LiveComponent/assets/src/live_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class extends Controller implements LiveController {

pollingIntervals: NodeJS.Timer[] = [];

isWindowUnloaded = false;
isConnected = false;

originalDataJSON = '{}';

Expand All @@ -86,7 +86,6 @@ export default class extends Controller implements LiveController {
pendingActionTriggerModelElement: HTMLElement|null = null;

initialize() {
this.markAsWindowUnloaded = this.markAsWindowUnloaded.bind(this);
this.handleUpdateModelEvent = this.handleUpdateModelEvent.bind(this);
this.handleInputEvent = this.handleInputEvent.bind(this);
this.handleChangeEvent = this.handleChangeEvent.bind(this);
Expand All @@ -100,6 +99,7 @@ export default class extends Controller implements LiveController {
}

connect() {
this.isConnected = true;
// hide "loading" elements to begin with
// This is done with CSS, but only for the most basic cases
this._onLoadingFinish();
Expand All @@ -111,7 +111,6 @@ export default class extends Controller implements LiveController {

this._initiatePolling();

window.addEventListener('beforeunload', this.markAsWindowUnloaded);
this._startAttributesMutationObserver();
this.element.addEventListener('live:update-model', this.handleUpdateModelEvent);
this.element.addEventListener('input', this.handleInputEvent);
Expand All @@ -125,7 +124,6 @@ export default class extends Controller implements LiveController {
this._stopAllPolling();
this.#clearRequestDebounceTimeout();

window.removeEventListener('beforeunload', this.markAsWindowUnloaded);
this.element.removeEventListener('live:update-model', this.handleUpdateModelEvent);
this.element.removeEventListener('input', this.handleInputEvent);
this.element.removeEventListener('change', this.handleChangeEvent);
Expand All @@ -137,6 +135,8 @@ export default class extends Controller implements LiveController {
if (this.mutationObserver) {
this.mutationObserver.disconnect();
}

this.isConnected = false;
}

/**
Expand Down Expand Up @@ -493,7 +493,7 @@ export default class extends Controller implements LiveController {
*/
#processRerender(html: string, response: Response) {
// check if the page is navigating away
if (this.isWindowUnloaded) {
if (!this.isConnected) {
return;
}

Expand Down Expand Up @@ -795,10 +795,6 @@ export default class extends Controller implements LiveController {
this._exposeOriginalData();
}

markAsWindowUnloaded = () => {
this.isWindowUnloaded = true;
};

handleConnectedControllerEvent(event: any) {
if (event.target === this.element) {
return;
Expand Down
36 changes: 34 additions & 2 deletions src/LiveComponent/assets/test/controller/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ describe('LiveController rendering Tests', () => {
.init();

test.controller.$render();
// imitate navigating away
fireEvent(window, createEvent('beforeunload', window));
// trigger disconnect
test.element.removeAttribute('data-controller')

// wait for the fetch to finish
await fetchMock.flush();
Expand All @@ -241,6 +241,38 @@ describe('LiveController rendering Tests', () => {
expect(test.element).not.toHaveTextContent('Hello');
});

it('renders if the page is navigating away and back', async () => {
const test = await createTest({greeting: 'aloha!'}, (data: any) => `
<div ${initComponent(data)}>${data.greeting}</div>
`);

test.expectsAjaxCall('get')
.expectSentData(test.initialData)
.serverWillChangeData((data) => {
data.greeting = 'Hello';
})
.delayResponse(100)
.init();

test.controller.$render();

// trigger controller disconnect
test.element.removeAttribute('data-controller')
// wait for the fetch to finish
await fetchMock.flush();

expect(test.element).toHaveTextContent('aloha!')

// trigger connect
test.element.setAttribute('data-controller', 'live')
test.controller.$render();
// wait for the fetch to finish
await fetchMock.flush();

// the re-render should have happened
expect(test.element).toHaveTextContent('Hello');
});

it('waits for the previous request to finish & batches changes', async () => {
const test = await createTest({ title: 'greetings', contents: '' }, (data: any) => `
<div ${initComponent(data, { debounce: 1 })}>
Expand Down