Adding history support
authorDavid Négrier <d.negrier@thecodingmachine.com>
Mon, 11 May 2020 22:07:50 +0000 (00:07 +0200)
committerDavid Négrier <d.negrier@thecodingmachine.com>
Tue, 12 May 2020 20:38:44 +0000 (22:38 +0200)
Arriving on a new map now changes the URL.
All URLs starting with _ are automatically redirected to index.html by the web server

front/dist/.htaccess [new file with mode: 0644]
front/dist/index.html
front/src/Phaser/Game/GameScene.ts
front/webpack.config.js

diff --git a/front/dist/.htaccess b/front/dist/.htaccess
new file mode 100644 (file)
index 0000000..eb3c6a4
--- /dev/null
@@ -0,0 +1,23 @@
+DirectoryIndex index.html
+
+# By default, Apache does not evaluate symbolic links if you did not enable this
+# feature in your server configuration. Uncomment the following line if you
+# install assets as symlinks or if you experience problems related to symlinks
+# when compiling LESS/Sass/CoffeScript assets.
+# Options FollowSymlinks
+
+# Disabling MultiViews prevents unwanted negotiation, e.g. "/index" should not resolve
+# to the front controller "/index.php" but be rewritten to "/index.php/index".
+<IfModule mod_negotiation.c>
+    Options -MultiViews
+</IfModule>
+
+RewriteEngine On
+
+RewriteBase /
+
+# If the requested filename exists, simply serve it.
+# We only want to let Apache serve files and not directories.
+# Rewrite all other queries starting with _ to index.ts.
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule "^_/" "/index.ts" [L]
index 85aced1e8d77384313737981f704ce46980cca94..51d527050d715f210485fe61b53dee1c9d9c016e 100644 (file)
@@ -5,6 +5,7 @@
         <meta name="viewport"
               content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
         <meta http-equiv="X-UA-Compatible" content="ie=edge">
+        <base href="/">
         <link rel="stylesheet" href="/resources/style/style.css">
         <title>Document</title>
     </head>
index bc6b746e85e85277fe5eac7660b74eedd4cc9983..d00ad6048474ab2dddf2ccbe7879357fc622adf4 100644 (file)
@@ -151,6 +151,15 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface, Creat
         context.strokeStyle = '#ffffff';
         context.stroke();
         this.circleTexture.refresh();
+
+        // Let's alter browser history
+        let url = new URL(this.MapUrlFile);
+        let path = '/_/'+url.host+url.pathname;
+        if (url.hash) {
+            // FIXME: entry should be dictated by a property passed to init()
+            path += '#'+url.hash;
+        }
+        window.history.pushState({}, null, path);
     }
 
     private getExitSceneUrl(layer: ITiledMapLayer): string|undefined {
index ff16480421fc3dd60efaaa3c04c70f04c54b1c6e..d1bccd6fed8f5384df232465f30ffa44de4ca2c8 100644 (file)
@@ -8,6 +8,12 @@ module.exports = {
         contentBase: './dist',
         host: '0.0.0.0',
         disableHostCheck: true,
+        historyApiFallback: {
+            rewrites: [
+                { from: /^_\/.*$/, to: '/index.html' }
+            ],
+            disableDotRule: true
+        },
     },
     module: {
         rules: [
@@ -24,6 +30,7 @@ module.exports = {
     output: {
         filename: 'bundle.js',
         path: path.resolve(__dirname, 'dist'),
+        publicPath: '/'
     },
     plugins: [
         new webpack.ProvidePlugin({