@vue-storefront/nuxt 2.4.5 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/module.js CHANGED
@@ -11,7 +11,7 @@ const rawSourcesModule = require('./modules/raw-sources-loader');
11
11
  module.exports = function VueStorefrontNuxtModule (moduleOptions) {
12
12
  const defaultOptions = {
13
13
  coreDevelopment: false,
14
- performance : {
14
+ performance: {
15
15
  httpPush: true,
16
16
  purgeCSS: {
17
17
  enabled: false,
@@ -76,10 +76,6 @@ module.exports = function VueStorefrontNuxtModule (moduleOptions) {
76
76
  });
77
77
  log.success('Installed Internationalization Cookies plugin');
78
78
 
79
- // Composition API plugin
80
- this.addModule('@nuxtjs/composition-api');
81
- log.success('Installed nuxt Composition API Module');
82
-
83
79
  // StorefrontUI module
84
80
  if (fs.existsSync(resolveDependency('@storefront-ui/vue'))) {
85
81
  storefrontUiModule.call(this, options);
@@ -92,6 +88,6 @@ module.exports = function VueStorefrontNuxtModule (moduleOptions) {
92
88
 
93
89
  // Raw sources loader
94
90
  rawSourcesModule.call(this, options);
95
- }
91
+ };
96
92
 
97
- module.exports.meta = require('../package.json')
93
+ module.exports.meta = require('../package.json');
@@ -1,5 +1,5 @@
1
1
 
2
- import { configureContext } from '@vue-storefront/core'
2
+ import { configureContext } from '@vue-storefront/core';
3
3
  import { useContext as useBaseContext } from '@nuxtjs/composition-api';
4
4
 
5
5
  const contextPlugin = (ctx, inject) => {
@@ -8,11 +8,11 @@ const contextPlugin = (ctx, inject) => {
8
8
  const useVSFContext = () => {
9
9
  const { $vsf, ...context } = useBaseContext();
10
10
 
11
- return { $vsf, ...context, ...$vsf }
12
- }
11
+ return { $vsf, ...context, ...$vsf };
12
+ };
13
13
 
14
14
  configureContext({ useVSFContext });
15
- inject('sharedRefsMap', sharedMap)
15
+ inject('sharedRefsMap', sharedMap);
16
16
  };
17
17
 
18
18
  export default contextPlugin;
@@ -1,39 +1,90 @@
1
- const { VSF_CURRENCY_COOKIE, VSF_COUNTRY_COOKIE } = require('@vue-storefront/core');
1
+ import { i18nRedirectsUtil, VSF_COUNTRY_COOKIE, VSF_CURRENCY_COOKIE, VSF_LOCALE_COOKIE } from '@vue-storefront/core';
2
2
 
3
- const i18nCookiesPlugin = ({ $cookies }) => {
4
- const i18n = <%= serialize(options) %>;
3
+ const i18nCookiesPlugin = ({ $cookies, i18n, app, redirect }) => {
4
+ const i18nOptions = <%= serialize(options) %>;
5
+ const isServer = typeof window === 'undefined';
6
+ const navigator = isServer ? undefined : window.navigator;
7
+ const acceptedLanguage = app.context.req?.headers?.['accept-language'] || navigator?.languages || '';
8
+ const isRouteValid = !!app.context.route.name;
9
+ const cookieNames = {
10
+ currency: i18nOptions.cookies?.currencyCookieName || VSF_CURRENCY_COOKIE,
11
+ country: i18nOptions.cookies?.countryCookieName || VSF_COUNTRY_COOKIE,
12
+ locale: i18nOptions.cookies?.localeCookieName || VSF_LOCALE_COOKIE
13
+ }
14
+ const cookieLocale = $cookies.get(cookieNames.locale);
15
+ const cookieCurrency = $cookies.get(cookieNames.currency);
16
+
17
+ const getCurrencyByLocale = (locale) =>
18
+ i18n.numberFormats?.[locale]?.currency?.currency
19
+ || i18nOptions.currency
20
+ || (i18nOptions.currencies.length && i18nOptions.currencies[0].name);
21
+
22
+ const utils = i18nRedirectsUtil({
23
+ path: app.context.route.path,
24
+ defaultLocale: i18nOptions.defaultLocale,
25
+ availableLocales: i18nOptions.locales.map((item) => item.code),
26
+ acceptedLanguages: isServer ? acceptedLanguage.split(',').map((item) => item.split(';')[0]) : acceptedLanguage,
27
+ cookieLocale
28
+ });
29
+
30
+ const targetLocale = utils.getTargetLocale();
31
+ const redirectPath = utils.getRedirectPath();
32
+
33
+ if (!isRouteValid) {
34
+ app.i18n.setLocale(targetLocale);
35
+ }
5
36
 
37
+ if (isServer) {
38
+ app.i18n.cookieValues = {
39
+ [cookieNames.locale]: targetLocale,
40
+ [cookieNames.currency]: getCurrencyByLocale(targetLocale)
41
+ };
42
+
43
+ if (redirectPath) {
44
+ redirect(redirectPath);
45
+ }
46
+
47
+ return;
48
+ }
49
+ const cookieOptions = {
50
+ path: '/',
51
+ sameSite: 'lax',
52
+ expires: new Date(new Date().setFullYear(new Date().getFullYear() + 1)) // Year from now
53
+ };
6
54
  const settings = {
7
- defaultLocale: i18n.defaultLocale || (i18n.locales.length && i18n.locales[0].code),
8
- currency: i18n.currency || (i18n.currencies.length && i18n.currencies[0].name),
9
- country: i18n.country || (i18n.countries.length && i18n.countries[0].name)
55
+ locale: targetLocale,
56
+ currency: getCurrencyByLocale(targetLocale),
57
+ country: i18nOptions.country || (i18nOptions.countries.length && i18nOptions.countries[0].name)
10
58
  };
11
59
 
12
60
  const missingFields = Object
13
61
  .entries(settings)
14
62
  .reduce((carry, [name, value]) => {
15
- !value && carry.push(name);
16
-
17
- return carry;
63
+ return [
64
+ ...carry,
65
+ ...(!value ? [name] : [])
66
+ ]
18
67
  }, []);
19
68
 
20
69
  if (missingFields.length) {
21
- throw new Error(`Following fields are missing in the i18n configuration: ${ missingFields.join(', ') }`);
70
+ throw new Error(`Following fields are missing in the i18n configuration: ${missingFields.join(', ')}`);
22
71
  }
23
72
 
24
- const cookieOptions = {
25
- path: '/',
26
- sameSite: 'lax',
27
- expires: new Date(new Date().setFullYear(new Date().getFullYear() + 1)) // Year from now
28
- };
73
+ if (cookieLocale !== settings.locale) {
74
+ $cookies.set(cookieNames.locale, settings.locale, cookieOptions);
75
+ }
29
76
 
30
- const cookieNames = {
31
- currency: i18n.cookies?.currencyCookieName || VSF_CURRENCY_COOKIE,
32
- country: i18n.cookies?.countryCookieName || VSF_COUNTRY_COOKIE
77
+ if (cookieCurrency !== settings.currency) {
78
+ $cookies.set(cookieNames.currency, settings.currency, cookieOptions);
33
79
  }
34
80
 
35
- !$cookies.get(cookieNames.currency) && $cookies.set(cookieNames.currency, settings.currency, cookieOptions);
36
81
  !$cookies.get(cookieNames.country) && $cookies.set(cookieNames.country, settings.country, cookieOptions);
37
- };
82
+
83
+ i18n.onBeforeLanguageSwitch = (oldLocale, newLocale, isInitialSetup, context) => {
84
+ $cookies.set(cookieNames.locale, newLocale, cookieOptions);
85
+ $cookies.set(cookieNames.currency, getCurrencyByLocale(newLocale), cookieOptions);
86
+ window.location.href = context.route.fullPath;
87
+ }
88
+ }
38
89
 
39
90
  export default i18nCookiesPlugin;
@@ -1,16 +1,16 @@
1
- import { configureSSR } from '@vue-storefront/core'
1
+ import { configureSSR } from '@vue-storefront/core';
2
2
  import { ssrRef, getCurrentInstance, onServerPrefetch } from '@nuxtjs/composition-api';
3
3
 
4
4
  const hasRouteChanged = (ctx) => {
5
- const { from } = ctx.$router.app.context;
6
- const { current } = ctx.$router.history
5
+ const { from } = ctx.proxy.$router.app.context;
6
+ const { current } = ctx.proxy.$router.history;
7
7
 
8
8
  if (!from) {
9
- return false
9
+ return false;
10
10
  }
11
11
 
12
- return from.fullPath !== current.fullPath
13
- }
12
+ return from.fullPath !== current.fullPath;
13
+ };
14
14
 
15
15
  const ssrPlugin = () => {
16
16
  configureSSR({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vue-storefront/nuxt",
3
- "version": "2.4.5",
3
+ "version": "2.5.2",
4
4
  "description": "",
5
5
  "main": "lib/module.js",
6
6
  "scripts": {
@@ -10,8 +10,7 @@
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@nuxt/typescript-build": "^2.0.0",
13
- "@vue/composition-api": "1.0.0-beta.21",
14
- "@nuxtjs/composition-api": "0.17.0",
13
+ "@nuxtjs/composition-api": "^0.29.3",
15
14
  "@nuxtjs/style-resources": "^1.0.0",
16
15
  "chalk": "^2.4.2",
17
16
  "chokidar": "^3.3.1",
@@ -20,7 +19,7 @@
20
19
  "nuxt-purgecss": "^1.0.0"
21
20
  },
22
21
  "devDependencies": {
23
- "@nuxt/types": "^0.7.9"
22
+ "@nuxt/types": "^2.15.8"
24
23
  },
25
24
  "publishConfig": {
26
25
  "access": "public"
@@ -1,4 +0,0 @@
1
- import Vue from 'vue';
2
- import VueCompositionApi from '@vue/composition-api';
3
-
4
- Vue.use(VueCompositionApi);