marzeelabs / besugo

Boilerplate for MZ version of hugo + netlifyCMS
https://besugo.marzeelabs.org
10 stars 0 forks source link
hugo jamstack netlify netlify-cms static-site-generator

Besugo

An awesome light-weight boilerplate to build an awesome static site, integrating Netlify CMS to be able to edit content, committing changes directly in Github and triggering new awesome live builds immediately. It's awesome!

Examples out there in the world

Requirements

Below are listed the versions used in Netlify to create the live, preview and branch builds. Ideally you'd be using the exact same versions locally, although there's some wiggle room of course.

  1. homebrew - a package manager for macOS, to manage applications such as hugo and yarn listed below.

    • Install:
      /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    • If after running the above command it complains that xcode alone is not sufficient on sierra, try running xcode-select --install. Note that macOS Sierra 10.12.6 or higher and XCode 9.2 are required.
  2. hugo v0.53 - builds the structure of the website based on the content setup, although we're phasing it out in favor of a fully JS/React solution. Any version over 0.27.1 should still work though.

    • Install:
      brew install hugo
    • Update:
      brew update && brew upgrade hugo
  3. yarn v1.12.3 - package/dependency manager. Any version of yarn above 0.27.5 should still work though.

    • Install:
      brew install yarn --without-node
    • Update:
      brew update && brew upgrade yarn
    • List installed yarn versions:
      brew list yarn --versions
    • Switch between yarn versions (global change):
      brew switch yarn 1.12.3
  4. nvm v0.33.11 - (not used by Netlify), very useful to manage your locale Node.js versions.

    • Install or update:

      curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
    • After installing you may need to re-open the terminal or run this for it to recognize the nvm command (repeat for every terminal you currently have open in which you want to use it):

      source ~/.bash_profile
  5. Node.js v8.12.0 LTS (lts/carbon) - our JavaScript runtime, we need it, for everything. Any Node v8.x.x or even v6.x.x (lts/boron) should still work though.

    • Install or update:
      nvm install lts/carbon

      Or to immediately install the global packages from another version (including system):

      nvm install lts/carbon --reinstall-packages-from=[SOME_VERSION]
    • I like aliases:
      nvm alias 8 lts/carbon
    • Switch version in the current terminal session:
      nvm use 8

Install and Run

  1. Clone project locally

  2. Install dependencies:

    yarn install
  3. (Optional, recommended) change the port in package.json, to avoid having other Besugo projects being served on the same port, for easier window/tab management during development.

  4. Serve on localhost via webpack and listen for changes (hot reloads in most cases):

    yarn start
  5. In your browser navigate to http://localhost:1313; don't forget to change the port to whatever you set in bullet point 3.

Upgrade projects (gulp, pre-yarn/webpack)

  1. Rebuild all dependencies; also for any time you need to rebuild node_modules from scratch, such as when upgrading node or yarn:

    yarn rebuild

    (shortcut to rm -rf node_modules & yarn install);

  2. Remove leftover js files in static folder:

    • static/css/cms-override.css
    • static/js/site.min.js
    • static/js/main.min.js
    • static/js/app.min.js
    • static/admin/main.min.js
  3. (Resume from bullet point 3 in the Install and Run section above)

From the Boilerplate to an Actual Project

Content

Content is of course the main focus of the website. Content types are defined in the file cms/config.yml and content entries are saved in the directory content as .md files.

Layouts

The folder layouts contains .html files defining, you guessed it, the layout of that content.

i18n

Components

The joy of using our boilerplate!

React components bring together the main website and Netlify CMS's interface, by using the same components to render each content type for the webpage (using server-side rendering to provide full markup on request), and live previewing content as you edit it in the CMS interface, so you can see exactly how it will look after saving.

Check out some code samples here and documentation.

Responsive images

During build, the script uses sharp, a high performance Node.js image processing library, to create copies of several sizes of any images uploaded to the media_folder (see Using Netlify CMS below). These are an integral part of using a Responsive Design approach, to optimize traffic and page load times on different devices.

We can use jpg (and the jpeg variant), png, webp and tiff file types.

To take advantage of these, you must first define in package.json what sizes and suffixes (to be appended to the original filename) sharp should work with; e.g.:

"sharp-config": {
  "src": "static/media",
  "dest": "public/media",
  "quality": 80,
  "sizes": [
    {
      "suffix": "-large",
      "width": 1400
    },
    {
      "suffix": "-regular",
      "width": 820
    },
    {
      "suffix": "-medium",
      "width": 680
    },
    {
      "suffix": "-small",
      "width": 460
    }
  ]
}

(Note: only change the src, dest, and quality if you also changed the media_folder value in Netlify CMS's configuration, or if you know what you're doing.)

Then to use the processed images themselves, either reference them directly in the code, or:

The component will then build the srcset attribute automatically, and the image node will load the file that best fits each device conditions. Browsers that don't support this feature will simply ignore the srcset attribute and use the original file set in src.

Check out MDN's article on Responsive Images for more information about these attributes and how to best use them.

You can also use responsive images in backgrounds through the SrcSetBg component. Just insert it inside the element to have the background image, configure its attributes in the same way as above for SrcSet, and it will do its magic.

You can optionally insert SrcSetBg anywhere in the document and set a bg-active-element attribute, with a query string pointing to the element that should have the background (id, class name, etc).

JavaScript

Most JavaScript dynamics should stay within the react components, since they're JavaScript already and know how to manage themselves.

Stylesheets

We use SASS to extend CSS and fulfill all our styling needs. Any .scss in the scss directory whose name doesn't start with an _ will be compiled and minified into public/css/[PATH-TO-FILE].css.

Static Files

All files to be copied as-is into the website go in the static folder.

Deploy on Netlify

  1. Push your Hugo project to Github

  2. Get a Netlify account

  3. Choose your Github repository and branch, use build command yarn build and set public/ as your Public Folder.

  4. (Optional) to automatically deploy all branches and PRs to unique urls, in Netlify's control panel, go to settings - build & deploy - continuous deployment and set Branch deploys to Deploy all branches.

Activate Netlify CMS

  1. Set the correct repo and branch on the provided static/admin/config.yml

  2. Create a new Github application following Netlify's instructions - don't forget the https://api.netlify.com/auth/done callback URL.

  3. Go to your Netlify dashboard, select your site, navigate to Access > Authentication Providers > Install Provider > Github and use the Client ID and Secret generated in step 2.

  4. Start using the CMS on http://[your-website-url]/admin/

A known issue with some setups is if you type only /admin, the Netlify script will garble up the url, sending you to /admin#/ and the CMS errors. This is an issue with webpack (no such problem with live Netlify builds), and to avoid it, always include the trailing slash in the /admin/ path.

Using Netlify CMS