Skip to content
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

Fixed modal caching issue when navigating back and autofocus on open #2959

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 7 additions & 8 deletions public/css/orchid.rtl.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/css/orchid.rtl.css.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/manifest.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions public/js/orchid.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/orchid.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/vendor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/js/vendor.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions public/mix-manifest.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions resources/js/controllers/application_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,33 @@ export default class ApplicationController extends Controller {
});
}


/**
* Updates or removes the Turbo cache control meta tag dynamically.
*
* @param {boolean|string} value - If `true`, sets "no-cache". If `false`, removes the meta tag.
* If a string is provided, sets it as the meta content.
*/
updateTurboCacheControl(value) {
const metaName = 'turbo-cache-control';
let metaTag = document.querySelector(`meta[name="${metaName}"]`);

if (value === false) {
// Remove the meta tag if explicitly disabling cache control
if (metaTag) {
metaTag.remove();
}
return;
}

if (!metaTag) {
metaTag = document.createElement('meta');
metaTag.name = metaName;
document.head.appendChild(metaTag);
}

metaTag.content = value === true ? 'no-cache' : String(value);
}


}
24 changes: 8 additions & 16 deletions resources/js/controllers/modal_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ export default class extends ApplicationController {
* Setup event listeners and initial modal state when the controller connects.
*/
connect() {
// Add event listeners for modal show and hide events
this.element.addEventListener('shown.bs.modal', this.show);
this.element.addEventListener('hide.bs.modal', this.hidden);

// Show the modal if the 'open' value is true
if (this.openValue) {
(new Modal(this.element)).show();
}

// Add event listeners for modal show and hide events
this.element.addEventListener('shown.bs.modal', this.show);
this.element.addEventListener('hide.bs.modal', this.hidden);

// Open the last opened modal if validation errors are present
this.openLastModal();
}
Expand All @@ -66,13 +66,7 @@ export default class extends ApplicationController {
* @param event - The event object for the 'shown.bs.modal' event.
*/
show(event) {
// Modify the backdrop to ensure it's not mistakenly identified as a Turbo frame
let backdrop = document.querySelector('.modal-backdrop');

if (backdrop !== null) {
backdrop.id = 'backdrop';
backdrop.dataset.turboTemporary = true;
}
this.updateTurboCacheControl('no-cache');

// Focus on the element with 'autofocus' attribute, if available
this.element.querySelector('[autofocus]')?.focus();
Expand All @@ -82,11 +76,9 @@ export default class extends ApplicationController {
* Handle the modal hide event.
* @param event - The event object for the 'hide.bs.modal' event.
*/
hidden(event) {
// Ensure the modal has appropriate classes when hiding
if (!this.element.classList.contains('fade')) {
this.element.classList.add('fade', 'in');
}
hidden(event) {
this.updateTurboCacheControl('cache');

// Clear the stored last open modal from session storage
this.clearLastOpenModal();
}
Expand Down
1 change: 1 addition & 0 deletions resources/views/partials/search-modal.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="modal fade center-scale"
tabindex="-1"
data-controller="modal"
role="dialog"
id="search-modal"
>
Expand Down
Loading