|
| 1 | +# multisite-module |
| 2 | +[](https://npmjs.com/package/multisite-module) |
| 3 | +[](https://npmjs.com/package/multisite-module) |
| 4 | +[](https://circleci.com/gh/ax2inc/multisite-module) |
| 5 | +[](https://codecov.io/gh/ax2inc/multisite-module) |
| 6 | +[](https://david-dm.org/ax2inc/multisite-module) |
| 7 | +[](https://github.com/airbnb/javascript) |
| 8 | + |
| 9 | + |
| 10 | +> Multisite features for your Nuxt project |
| 11 | +
|
| 12 | +[📖 **Release Notes**](./CHANGELOG.md) |
| 13 | + |
| 14 | +## Features |
| 15 | + |
| 16 | +This module helps you bring multisite features to your Nuxt project. Here are the main features: |
| 17 | + |
| 18 | +- Current site detection based on host (or query string in development) |
| 19 | +- Contextual CSS vars declaration for site-specific theming |
| 20 | +- Contextual meta data |
| 21 | + |
| 22 | +## Setup |
| 23 | + |
| 24 | +- Install the module with your favorite package manager. |
| 25 | + |
| 26 | +```sh |
| 27 | +yarn add multisite-module |
| 28 | +# Or npm i multisite-module |
| 29 | +``` |
| 30 | + |
| 31 | +- Add `multisite-module` to `modules` section of `nuxt.config.js`. |
| 32 | + |
| 33 | +```js |
| 34 | +// nuxt.config.js |
| 35 | + |
| 36 | +{ |
| 37 | + modules: [ |
| 38 | + 'multisite-module', |
| 39 | + ], |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +- Configure the module as needed by adding a `multisite` key to `nuxt.config.js`. |
| 44 | + |
| 45 | +```js |
| 46 | +// nuxt.config.js |
| 47 | + |
| 48 | +{ |
| 49 | + multisite: { |
| 50 | + // Module options |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | + |
| 56 | +## Options |
| 57 | + |
| 58 | +### sites |
| 59 | + |
| 60 | +- Type: `Array` |
| 61 | + |
| 62 | +List of sites. |
| 63 | + |
| 64 | +```js |
| 65 | +{ |
| 66 | + multisite: { |
| 67 | + sites: [ |
| 68 | + { |
| 69 | + id: 'my-site', |
| 70 | + title: 'My awesome site', |
| 71 | + isDefault: true, |
| 72 | + hostPatterns: 'myawesomesite\.com,myincrediblesite\.(com|org)', |
| 73 | + cssVars: { |
| 74 | + '--primary-color': '#41B883', |
| 75 | + '--secondary-color': '#3B8070', |
| 76 | + }, |
| 77 | + head: { |
| 78 | + link: [ |
| 79 | + { rel: 'icon', type: 'image/x-icon', href: '/my-site/favicon.ico' }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +Each item in `sites` can have a few options of its own: |
| 89 | + |
| 90 | +#### id |
| 91 | + |
| 92 | +- Type: `Integer|String` |
| 93 | + |
| 94 | +The site's unique identifier. |
| 95 | + |
| 96 | +#### isDefault |
| 97 | + |
| 98 | +- Type: `Boolean` |
| 99 | + |
| 100 | +Wether this site should be considered as the default one. Any request that cannot be resolved to one of the sites will fallback to the default one. |
| 101 | + |
| 102 | +#### hostPatterns |
| 103 | + |
| 104 | +- Type: `String` |
| 105 | + |
| 106 | +A list of comma-separated patterns to test against requests host in order to enable this site in production. |
| 107 | + |
| 108 | +#### cssVars |
| 109 | + |
| 110 | +- Type: `Object` |
| 111 | + |
| 112 | +CSS vars that should be set when visiting this site. |
| 113 | + |
| 114 | +#### head |
| 115 | + |
| 116 | +- Type: `Object` |
| 117 | + |
| 118 | +This is the same as Nuxt's [head property](https://nuxtjs.org/api/configuration-head#the-head-property), options defined here are merged with the main `head` property definition. |
| 119 | + |
| 120 | +> NOTE: Functions are not supported here |
| 121 | +
|
| 122 | +## Usage |
| 123 | + |
| 124 | +### Development |
| 125 | + |
| 126 | +In development, switch from one site to another by adding a `site` query parameter to the URL. The value should be the site's ID as defined in the module's configuration. ie: [http://127.0.0.1:8080/?site=my-site](http://127.0.0.1:8080/?site=my-site) |
| 127 | + |
| 128 | +Active site is stored in a cookie, so next time you visit [http://127.0.0.1:8080](http://127.0.0.1:8080), active site will be last used one. |
| 129 | + |
| 130 | +### Production |
| 131 | + |
| 132 | +In production, active site is detected by matching request host against the patterns you defined in `hostPatterns` options. ie if you visit [http://myawesomesite.com](http://myawesomesite.com), `my-site` will be set as active site. |
| 133 | + |
| 134 | +A `$multisite` property is added to the app's context, it contains a few helpers that you can use in any component. |
| 135 | + |
| 136 | +### Properties |
| 137 | + |
| 138 | +#### site |
| 139 | + |
| 140 | +- Type: `Object` |
| 141 | + |
| 142 | +The `site` property contains current site's configuration. You could use it to display the current site's title: |
| 143 | + |
| 144 | +```vue |
| 145 | +<template> |
| 146 | + <h1 class="site-title"> |
| 147 | + {{ $multisite.site.title }} |
| 148 | + </h1> |
| 149 | +</template> |
| 150 | +``` |
| 151 | + |
| 152 | +### Methods |
| 153 | + |
| 154 | +#### asset |
| 155 | + |
| 156 | +- Arguments |
| 157 | + - `{String} path`: required |
| 158 | + - `{Integer|String} site`: optional, defaults to current site ID |
| 159 | +- Return: `String` |
| 160 | + |
| 161 | +Get an asset's path for given site. If no site is specified, defaults to active site. |
| 162 | + |
| 163 | +```vue |
| 164 | +<template> |
| 165 | + <header> |
| 166 | + <img class="logo" :src="$multisite.asset('logo.png')"> |
| 167 | + <!-- Renders to <img class="logo" src="/my-site/logo.png"> --> |
| 168 | + </header> |
| 169 | +</template> |
| 170 | +``` |
| 171 | + |
| 172 | +> NOTE: It's recommended that you place site-specific assets in a directory named after the site's ID as defined in the module's options. Sites assets directories should be in the static/ directory. |
| 173 | +
|
| 174 | +## License |
| 175 | + |
| 176 | +[MIT License](./LICENSE) |
| 177 | + |
| 178 | +Copyright (c) Ax2 Inc. |
0 commit comments