Keep app installation discoverable when browser prompts are unavailable

This commit is contained in:
Nick Beckley 2026-10-28 20:46:25 +00:00
parent db7d3b340e
commit 8ac01000ec
4 changed files with 42 additions and 4 deletions

View File

@ -9,7 +9,7 @@ const server = http.createServer((req, res) => {
res.setHeader('Cache-Control', 'no-cache');
if (req.url === '/fixture') {
res.setHeader('Content-Type', 'text/html');
res.end(`<html data-app-theme="modern-light"><head><meta name="theme-color" content="#fff"><link rel="manifest" href="/site.webmanifest"><link rel="stylesheet" href="/css/application-shell.css"></head><body><input id="editor"><div hidden data-pwa-install-item><button data-pwa-install>Install</button></div><span data-pwa-status></span><script src="/js/application-pwa.js"></script></body></html>`);
res.end(`<html data-app-theme="modern-light"><head><meta charset="utf-8"><meta name="theme-color" content="#fff"><link rel="manifest" href="/site.webmanifest"><link rel="stylesheet" href="/css/application-shell.css"></head><body><input id="editor"><div hidden data-pwa-install-item><button data-pwa-install>Install</button></div><span data-pwa-status></span><script src="/js/application-pwa.js"></script></body></html>`);
} else if (req.url === '/service-worker.js') {
res.setHeader('Content-Type', 'application/javascript');
res.end(fs.readFileSync(path.join(root, 'service-worker.js'), 'utf8') + `\n// test deployment ${workerRevision}`);
@ -60,6 +60,8 @@ const server = http.createServer((req, res) => {
await context.setOffline(true);
assert.equal(await page.evaluate(() => fetch('/private-data', { cache: 'no-store' }).then(() => false, () => true)), true);
await context.setOffline(false);
assert.equal(await page.locator('[data-pwa-install-item]').isVisible(), true);
assert.equal(await page.locator('[data-pwa-install]').textContent(), 'App installation…');
// Browser supplies this event only when installable; exercise one-shot UI handling.
await page.evaluate(() => {
const event = new Event('beforeinstallprompt', { cancelable: true });
@ -70,6 +72,9 @@ const server = http.createServer((req, res) => {
assert.equal(await page.locator('[data-pwa-install-item]').isVisible(), true);
await page.click('[data-pwa-install]');
assert.equal(await page.evaluate(() => window.prompted), true);
assert.equal(await page.locator('[data-pwa-install-item]').isVisible(), true);
assert.equal(await page.locator('[data-pwa-install]').textContent(), 'App installation…');
await page.evaluate(() => window.dispatchEvent(new Event('appinstalled')));
assert.equal(await page.locator('[data-pwa-install-item]').isVisible(), false);
console.log('PASS PWA: manifest/icons, four theme metadata values, real worker activation/update without lost edits, no Cache Storage, private data unavailable offline, install action.');
} finally { await browser.close(); await new Promise(resolve => server.close(resolve)); }

View File

@ -146,3 +146,22 @@
</div>
<form hidden data-appearance-form data-theme-url="@Url.Action("Theme", "Appearance")" data-ribbon-url="@Url.Action("Ribbon", "Appearance")">@Html.AntiForgeryToken()</form>
</section>
<div class="modal fade" id="pwaInstallationHelp" tabindex="-1" aria-labelledby="pwaInstallationTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title fs-5" id="pwaInstallationTitle">Install PlotDirector</h2>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>Open PlotDirector in its own application window using your browser’s installation menu.</p>
<p><strong>Google Chrome:</strong> open the ⋮ menu, then <strong>Cast, save, and share → Install page as app…</strong></p>
<p><strong>Microsoft Edge:</strong> open the … menu, then <strong>More tools → Apps → Install this site as an app</strong>.</p>
<p>The address-bar install icon may also appear. When your browser offers installation directly, the account menu will show <strong>Install PlotDirector…</strong>.</p>
<p class="small text-muted mb-0">If installation is unavailable, check whether PlotDirector is already installed in this browser. Private browsing or your organisation’s browser settings may limit installation. PlotDirector requires an internet connection.</p>
</div>
<div class="modal-footer"><button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button></div>
</div>
</div>
</div>

View File

@ -5,10 +5,16 @@
const status = document.querySelector('[data-pwa-status]');
const standalone = window.matchMedia('(display-mode: standalone)');
let installPrompt = null;
let installedThisSession = false;
function updateMode() {
const installed = standalone.matches || navigator.standalone === true;
root.dataset.standalone = String(installed);
if (installItem) installItem.hidden = installed || !installPrompt;
if (installItem) installItem.hidden = installed || installedThisSession;
if (installButton) {
installButton.textContent = installPrompt ? 'Install PlotDirector…' : 'App installation…';
if (installPrompt) installButton.removeAttribute('aria-haspopup');
else installButton.setAttribute('aria-haspopup', 'dialog');
}
}
function updateTheme() {
// The shell's tokens remain the only palette; theme changes include save rollback.
@ -24,12 +30,20 @@
updateMode();
});
window.addEventListener('appinstalled', () => {
installedThisSession = true;
installPrompt = null;
updateMode();
if (status) status.textContent = 'PlotDirector installed';
});
installButton?.addEventListener('click', async () => {
if (!installPrompt) return;
if (!installPrompt) {
const help = document.getElementById('pwaInstallationHelp');
if (help && window.bootstrap) {
help.addEventListener('hidden.bs.modal', () => document.getElementById('accountMenuButton')?.focus(), { once: true });
bootstrap.Modal.getOrCreateInstance(help).show();
}
return;
}
const prompt = installPrompt;
installPrompt = null;
updateMode();

View File

@ -24,7 +24,7 @@ The worker calls `skipWaiting` and `clients.claim` without forcing any page relo
## Installation and themes
Chromium's native install UI remains available. The account menu's small “Install PlotDirector…” button appears only after a real `beforeinstallprompt` event and outside standalone mode. The event is consumed once; dismissal does not trigger repeated prompts. `appinstalled` and `(display-mode: standalone)` changes hide the command. Unsupported browsers get no inactive installation control. Installation failure does not block authoring.
Chromium's native install UI remains available. The account menu's small “Install PlotDirector…” button appears only after a real `beforeinstallprompt` event and outside standalone mode. The event is consumed once; dismissal does not trigger repeated prompts. `appinstalled` and `(display-mode: standalone)` changes hide the command. When no native prompt is available, the account menu instead offers “App installation…” with browser-menu instructions. This help action does not imply that a native prompt is available; it keeps installation discoverable when browsers delay or suppress promotion. Both actions hide in standalone mode and after installation in the current document. Installation failure does not block authoring.
The same shell renders in standalone mode, with no added browser controls or fixed window dimensions. `data-standalone` records detection for future modest integration needs. iOS's `navigator.standalone` is also recognised.