Skip to content

Commit 82e34f0

Browse files
committed
Warn on file:// loads using PuterDialog
1 parent 9e4c3f8 commit 82e34f0

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/puter-js/src/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { PTLSSocket } from './modules/networking/PTLS.js';
1818
import { pFetch } from './modules/networking/requests.js';
1919
import OS from './modules/OS.js';
2020
import Perms from './modules/Perms.js';
21+
import PuterDialog from './modules/PuterDialog.js';
2122
import UI from './modules/UI.js';
2223
import Util from './modules/Util.js';
2324
import { WorkersHandler } from './modules/Workers.js';
@@ -505,6 +506,11 @@ const puterInit = (function () {
505506

506507
// Print a CTA for developers to publish their app on the Puter App Store
507508
this.printDevCTA();
509+
510+
// If the page was opened directly from disk (file:// protocol),
511+
// Puter.js cannot function. Warn the developer immediately on
512+
// load rather than waiting for an action that triggers auth.
513+
this.warnUnsupportedProtocol();
508514
} else if ( this.env === 'web-worker' || this.env === 'service-worker' || this.env === 'nodejs' ) {
509515
this.initSubmodules();
510516
}
@@ -916,6 +922,33 @@ const puterInit = (function () {
916922
);
917923
};
918924

925+
/**
926+
* Shows the "Unsupported Protocol" warning dialog when the SDK is
927+
* loaded directly from the file:// protocol. Runs once on load (when
928+
* the DOM is ready) so the developer is told to use a web server
929+
* immediately, instead of only when an action triggers the auth flow.
930+
* @private
931+
*/
932+
warnUnsupportedProtocol = function () {
933+
if ( globalThis.location?.protocol !== 'file:' ) return;
934+
if ( this._fileProtocolWarned ) return;
935+
this._fileProtocolWarned = true;
936+
937+
const showDialog = () => {
938+
// On file:// PuterDialog renders the "Unsupported Protocol"
939+
// warning instead of the auth consent content.
940+
const dialog = new PuterDialog(() => {}, () => {});
941+
document.body.appendChild(dialog);
942+
dialog.open();
943+
};
944+
945+
if ( document.readyState === 'loading' ) {
946+
document.addEventListener('DOMContentLoaded', showDialog, { once: true });
947+
} else {
948+
showDialog();
949+
}
950+
};
951+
919952
/**
920953
* Checks and updates the GUI FS cache for most-commonly used paths
921954
* @private

0 commit comments

Comments
 (0)