vue-ssr-starter
vue-ssr-starter copied to clipboard
Starter kit for projects with Webpack 4, Vue 2 and SSR
Webpack, Vue, SSR project template
Includes:
- Webpack 4
- polka web-server
- Vue 2 with SSR, Vuex and vue-loader
- Stylus with kouto-swiss
- Axios
- Pug
- SVG sprites builder
- ESlint with pre-push hook
Getting started
npm i
# development server on localhost:8080
npm run dev
# production build
npm run build
# production server on localhost:8080
npm start
Configuration
.env.dev contains environment variables used for local development. You can change application port,
API base URL for server and client and enable/disable proxy (http-proxy-middleware).
For production builds you should provide same environment variables yourself.
Alternatively you can use .env after these steps:
- Move
dotenvfromdevDependenciestodependencies. - Create
.envfile with production config. - Run
npm startorNODE_ENV=production node -r dotenv/config index.
API proxy
See setup-proxy.js for description.
Application structure
index.js- application serversetup-proxy.js-http-proxy-middlewaresetupbuild/- build related codesetup-dev-server- development server setupsvg-sprite- svg sprite generation script, gathers icons fromsrc/assets/svg-iconsand compiles them intosrc/assets/sprite.svgwebpack/- webpack config,base- common,serverfor server with SSR,clientfor browser
dist/- production build filessrc/assets/- application static assets (images, fonts, icons etc.)sprite.svg- generated sprites file,require('src/assets/sprite.svg')will return file contents stringfonts/- guess whatimages/- static images (backgrounds, patterns etc.)svg-icons/- contains SVG icons for the sprite
entry/- main entry pointsapp- shared between server and client, exports a factory function returning root component instance, mixes it withapp.vueclient- client entryserver- server entry
components/- vue componentspages/- components here are implicitly attached to routes same with componets' file names (excluding leading_in file or folder names and404.vuewhich will be used as a catch-all route)filters/- vue filters registered implicitly viaVue.filter()directives/- vue directives registered implicitly viaVue.directive()store/- Vuex storage,indexreturns a factory function returning configured Vuex store instanceutils/- common utility functionsindex- common utility functionsssr- SSR related functions and mixins
app.vue- application root component, implicitly mixed withentry\apphttp- exports http client instance (Axios)layout.pug- application HTML layoutrouter- exports a factory function returning vue-router instanceshared.styl- globally included stylus file (for variables, mixins, etc.)
SSR related component features
Every component within src/pages directory can use some special features providing full SSR support:
-
component.routePath, String - additional route suffix. Usually used to provide dynamic route segments. You can use any string allowed for the vue-router path definition. All dynamic segments are automatically mapped to componentprops. -
component.routeMeta, Object -route.meta. IncludestatusCodehere to modify an HTTP status returned with SSR. 404 route includes 404 status code by default. -
component.prefetch({ store, props, route }), function (store- vuex store instance,props- route params,route- current route object).Must return a promise. Allows some async routine before actual application rendering on server side. To pass any data to component: resolve promise with needed data and add corresponding
component.datafields with their initial values to prevent ...property or method not defined... error. Automatically called on client side frombeforeMountandbeforeRouteChangehooks as well. Seesrc/utils/ssrmixin. -
Boolean
prefetchingdata field indicates when prefetch is running.IMPORTANT: there is no component context within
prefetchfunction because component instance is not created yet! That means you should not try to usethis.
prefetch also works on the root component (src/app.vue) with some restrictions:
- no way to pass component data (only store can be affected).