Browse Source

add router middleware

Olivier Massot 4 năm trước cách đây
mục cha
commit
24a5245657
2 tập tin đã thay đổi với 23 bổ sung0 xóa
  1. 19 0
      middleware/query_preserver.js
  2. 4 0
      nuxt.config.js

+ 19 - 0
middleware/query_preserver.js

@@ -0,0 +1,19 @@
+/**
+ * This middleware preserve some global query parameters during the navigation,
+ * like the theme color or the parent organization
+ */
+
+const PRESERVE = ['parent', 'theme', 'view']
+
+export default function ({ from, route, redirect }) {
+  let redirected = false
+  for (const param in from.query) {
+    if (PRESERVE.includes(param) && !(param in route.query)) {
+      route.query[param] = from.query[param]
+      redirected = true
+    }
+  }
+  if (redirected) {
+    redirect(route)
+  }
+}

+ 4 - 0
nuxt.config.js

@@ -76,6 +76,10 @@ export default {
     ]
   ],
 
+  router: {
+    middleware: ['query_preserver']
+  },
+
   // Vuetify module configuration
   // @see https://vuetifyjs.com/en/features/theme/
   vuetify: {