frontend.md 13.6 KB
Newer Older
1
# Frontend Development Guidelines
2

3
This document describes various guidelines to ensure consistency and quality
4
across GitLab's frontend team.
5

6 7
## Overview

8 9 10
GitLab is built on top of [Ruby on Rails][rails] using [Haml][haml] with
[Hamlit][hamlit]. Be wary of [the limitations that come with using
Hamlit][hamlit-limits]. We also use [SCSS][scss] and plain JavaScript with
11 12 13 14 15 16 17 18
[ES6 by way of Babel][es6].

The asset pipeline is [Sprockets][sprockets], which handles the concatenation,
minification, and compression of our assets.

[jQuery][jquery] is used throughout the application's JavaScript, with
[Vue.js][vue] for particularly advanced, dynamic elements.

19 20 21
### Vue

For more complex frontend features, we recommend using Vue.js. It shares
22
some ideas with React.js as well as Angular.
23 24 25

To get started with Vue, read through [their documentation][vue-docs].

26 27
## Performance

28 29 30 31 32 33 34
### Resources

- [WebPage Test][web-page-test] for testing site loading time and size.
- [Google PageSpeed Insights][pagespeed-insights] grades web pages and provides feedback to improve the page.
- [Profiling with Chrome DevTools][google-devtools-profiling]
- [Browser Diet][browser-diet] is a community-built guide that catalogues practical tips for improving web page performance.

35
### Page-specific JavaScript
36

37 38 39 40
Certain pages may require the use of a third party library, such as [d3][d3] for
the User Activity Calendar and [Chart.js][chartjs] for the Graphs pages. These
libraries increase the page size significantly, and impact load times due to
bandwidth bottlenecks and the browser needing to parse more JavaScript.
41

42
In cases where libraries are only used on a few specific pages, we use
43
"page-specific JavaScript" to prevent the main `application.js` file from
44 45 46 47 48 49
becoming unnecessarily large.

Steps to split page-specific JavaScript from the main `application.js`:

1. Create a directory for the specific page(s), e.g. `graphs/`.
1. In that directory, create a `namespace_bundle.js` file, e.g. `graphs_bundle.js`.
50
1. In `graphs_bundle.js` add the line `//= require_tree .`, this adds all other files in the directory to the bundle.
51
1. Add any necessary libraries to `app/assets/javascripts/lib/`, all files directly descendant from this directory will be precompiled as separate assets, in this case `chart.js` would be added.
52 53 54
1. Add the new "bundle" file to the list of precompiled assets in
`config/application.rb`.
  - For example: `config.assets.precompile << "graphs/graphs_bundle.js"`.
55
1. Move code reliant on these libraries into the `graphs` directory.
56 57 58 59 60 61 62 63
1. In the relevant views, add the scripts to the page with the following:

```haml
- content_for :page_specific_javascripts do
  = page_specific_javascript_tag('lib/chart.js')
  = page_specific_javascript_tag('graphs/graphs_bundle.js')
```

64
The above loads `chart.js` and `graphs_bundle.js` for this page only. `chart.js`
65
is separated from the bundle file so it can be cached separately from the bundle
66 67
and reused for other pages that also rely on the library. For an example, see
[this Haml file][page-specific-js-example].
68 69 70 71

### Minimizing page size

A smaller page size means the page loads faster (especially important on mobile
72 73
and poor connections), the page is parsed more quickly by the browser, and less
data is used for users with capped data plans.
74 75 76

General tips:

77
- Don't add new fonts.
78
- Prefer font formats with better compression, e.g. WOFF2 is better than WOFF, which is better than TTF.
79
- Compress and minify assets wherever possible (For CSS/JS, Sprockets does this for us).
80 81
- If some functionality can reasonably be achieved without adding extra libraries, avoid them.
- Use page-specific JavaScript as described above to dynamically load libraries that are only needed on certain pages.
82 83 84

## Accessibility

85 86
### Resources

87
[Chrome Accessibility Developer Tools][chrome-accessibility-developer-tools]
88 89 90 91 92 93 94
are useful for testing for potential accessibility problems in GitLab.

Accessibility best-practices and more in-depth information is available on
[the Audit Rules page][audit-rules] for the Chrome Accessibility Developer Tools.

## Security

95 96
### Resources

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
[Mozilla’s HTTP Observatory CLI][observatory-cli] and the
[Qualys SSL Labs Server Test][qualys-ssl] are good resources for finding
potential problems and ensuring compliance with security best practices.

<!-- Uncomment these sections when CSP/SRI are implemented.
### Content Security Policy (CSP)

Content Security Policy is a web standard that intends to mitigate certain
forms of Cross-Site Scripting (XSS) as well as data injection.

Content Security Policy rules should be taken into consideration when
implementing new features, especially those that may rely on connection with
external services.

GitLab's CSP is used for the following:

- Blocking plugins like Flash and Silverlight from running at all on our pages.
- Blocking the use of scripts and stylesheets downloaded from external sources.
- Upgrading `http` requests to `https` when possible.
- Preventing `iframe` elements from loading in most contexts.

Some exceptions include:

- Scripts from Google Analytics and Piwik if either is enabled.
- Connecting with GitHub, Bitbucket, GitLab.com, etc. to allow project importing.
- Connecting with Google, Twitter, GitHub, etc. to allow OAuth authentication.

We use [the Secure Headers gem][secure_headers] to enable Content
Security Policy headers in the GitLab Rails app.

Some resources on implementing Content Security Policy:

- [MDN Article on CSP][mdn-csp]
- [GitHub’s CSP Journey on the GitHub Engineering Blog][github-eng-csp]
- The Dropbox Engineering Blog's series on CSP: [1][dropbox-csp-1], [2][dropbox-csp-2], [3][dropbox-csp-3], [4][dropbox-csp-4]

### Subresource Integrity (SRI)

Subresource Integrity prevents malicious assets from being provided by a CDN by
guaranteeing that the asset downloaded is identical to the asset the server
is expecting.

The Rails app generates a unique hash of the asset, which is used as the
asset's `integrity` attribute. The browser generates the hash of the asset
on-load and will reject the asset if the hashes do not match.

All CSS and JavaScript assets should use Subresource Integrity. For implementation details,
see the documentation for [the Sprockets implementation of SRI][sprockets-sri].

Some resources on implementing Subresource Integrity:

- [MDN Article on SRI][mdn-sri]
- [Subresource Integrity on the GitHub Engineering Blog][github-eng-sri]

-->

153 154
### Including external resources

155 156 157 158 159
External fonts, CSS, and JavaScript should never be used with the exception of
Google Analytics and Piwik - and only when the instance has enabled it. Assets
should always be hosted and served locally from the GitLab instance. Embedded
resources via `iframes` should never be used except in certain circumstances
such as with ReCaptcha, which cannot be used without an `iframe`.
160 161 162

### Avoiding inline scripts and styles

163
In order to protect users from [XSS vulnerabilities][xss], we will disable inline scripts in the future using Content Security Policy.
164

165 166
While inline scripts can be useful, they're also a security concern. If
user-supplied content is unintentionally left un-sanitized, malicious users can
167
inject scripts into the web app.
168

169 170 171 172
Inline styles should be avoided in almost all cases, they should only be used
when no alternatives can be found. This allows reusability of styles as well as
readability.

173
## Style guides and linting
174

175
See the relevant style guides for our guidelines and for information on linting:
176 177 178

- [SCSS][scss-style-guide]

179 180
## Testing

181 182 183
Feature tests need to be written for all new features. Regression tests
also need to be written for all bug fixes to prevent them from occurring
again in the future.
184 185 186 187

See [the Testing Standards and Style Guidelines](testing.md) for more
information.

W
Winnie 已提交
188 189 190 191 192 193 194 195 196 197 198
### Running frontend tests

`rake teaspoon` runs the frontend-only (JavaScript) tests.
It consists of two subtasks:

- `rake teaspoon:fixtures` (re-)generates fixtures
- `rake teaspoon:tests` actually executes the tests

As long as the fixtures don't change, `rake teaspoon:tests` is sufficient
(and saves you some time).

199 200 201 202 203 204
If you need to debug your tests and/or application code while they're
running, navigate to [localhost:3000/teaspoon](http://localhost:3000/teaspoon)
in your browser, open DevTools, and run tests for individual files by clicking 
on them. This is also much faster than setting up and running tests from the 
command line.

W
Winnie 已提交
205 206 207
Please note: Not all of the frontend fixtures are generated. Some are still static
files. These will not be touched by `rake teaspoon:fixtures`.

208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
## Design Patterns

### Singletons

When exactly one object is needed for a given task, prefer to define it as a
`class` rather than as an object literal. Prefer also to explicitly restrict
instantiation, unless flexibility is important (e.g. for testing).

```
// bad

gl.MyThing = {
  prop1: 'hello',
  method1: () => {}
};

// good

class MyThing {
  constructor() {
    this.prop1 = 'hello';
  }
  method1() {}
}

gl.MyThing = new MyThing();

// best
B
Bryce Johnson 已提交
236

237 238 239 240 241
let singleton;

class MyThing {
  constructor() {
    if (!singleton) {
B
Bryce Johnson 已提交
242 243 244
      singleton = this;
      singleton.init();
    }
245 246 247 248 249 250 251 252 253 254 255
      return singleton;
  }

  init() {
    this.prop1 = 'hello';
  }

  method1() {}
}

gl.MyThing = MyThing;
B
Bryce Johnson 已提交
256

257 258
```

259 260 261 262
## Supported browsers

For our currently-supported browsers, see our [requirements][requirements].

263 264 265 266 267 268 269 270 271
[rails]: http://rubyonrails.org/
[haml]: http://haml.info/
[hamlit]: https://github.com/k0kubun/hamlit
[hamlit-limits]: https://github.com/k0kubun/hamlit/blob/master/REFERENCE.md#limitations
[scss]: http://sass-lang.com/
[es6]: https://babeljs.io/
[sprockets]: https://github.com/rails/sprockets
[jquery]: https://jquery.com/
[vue]: http://vuejs.org/
272
[vue-docs]: http://vuejs.org/guide/index.html
273 274 275 276
[web-page-test]: http://www.webpagetest.org/
[pagespeed-insights]: https://developers.google.com/speed/pagespeed/insights/
[google-devtools-profiling]: https://developers.google.com/web/tools/chrome-devtools/profile/?hl=en
[browser-diet]: https://browserdiet.com/
277 278
[d3]: https://d3js.org/
[chartjs]: http://www.chartjs.org/
279
[page-specific-js-example]: https://gitlab.com/gitlab-org/gitlab-ce/blob/13bb9ed77f405c5f6ee4fdbc964ecf635c9a223f/app/views/projects/graphs/_head.html.haml#L6-8
280 281
[chrome-accessibility-developer-tools]: https://github.com/GoogleChrome/accessibility-developer-tools
[audit-rules]: https://github.com/GoogleChrome/accessibility-developer-tools/wiki/Audit-Rules
282
[observatory-cli]: https://github.com/mozilla/http-observatory-cli
283 284 285 286 287 288 289 290 291 292 293
[qualys-ssl]: https://www.ssllabs.com/ssltest/analyze.html
[secure_headers]: https://github.com/twitter/secureheaders
[mdn-csp]: https://developer.mozilla.org/en-US/docs/Web/Security/CSP
[github-eng-csp]: http://githubengineering.com/githubs-csp-journey/
[dropbox-csp-1]: https://blogs.dropbox.com/tech/2015/09/on-csp-reporting-and-filtering/
[dropbox-csp-2]: https://blogs.dropbox.com/tech/2015/09/unsafe-inline-and-nonce-deployment/
[dropbox-csp-3]: https://blogs.dropbox.com/tech/2015/09/csp-the-unexpected-eval/
[dropbox-csp-4]: https://blogs.dropbox.com/tech/2015/09/csp-third-party-integrations-and-privilege-separation/
[mdn-sri]: https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity
[github-eng-sri]: http://githubengineering.com/subresource-integrity/
[sprockets-sri]: https://github.com/rails/sprockets-rails#sri-support
294
[xss]: https://en.wikipedia.org/wiki/Cross-site_scripting
295
[scss-style-guide]: scss_styleguide.md
296
[requirements]: ../install/requirements.md#supported-web-browsers
297

298
## Gotchas
299

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
### Spec errors due to use of ES6 features in `.js` files

If you see very generic JavaScript errors (e.g. `jQuery is undefined`) being 
thrown in Teaspoon, Spinach, or Rspec tests but can't reproduce them manually, 
you may have included `ES6`-style JavaScript in files that don't have the 
`.js.es6` file extension. Either use ES5-friendly JavaScript or rename the file 
you're working in (`git mv <file.js> <file.js.es6>`). 

### Spec errors due to use of unsupported JavaScript

Similar errors will be thrown if you're using JavaScript features not yet 
supported by our test runner's version of webkit, whether or not you've updated
the file extension. Examples of unsupported JavaScript features are:

- Array.from
- Array.find
- Array.first
- Object.assign
- Async functions
- Generators
- Array destructuring
- For Of
- Symbol/Symbol.iterator]
- Spread

Until these are polyfilled or transpiled appropriately, they should not be used. 
Please update this list with additional unsupported features or when any of 
these are made usable.

### Spec errors due to JavaScript not enabled

If, as a result of a change you've made, a feature now depends on JavaScript to 
run correctly, you need to make sure a JavaScript web driver is enabled when
specs are run. If you don't you'll see vague error messages from the spec 
runner, and an explosion of vague console errors in the HTML snapshot. 

To enable a JavaScript driver in an `rspec` test, add `js: true` to the 
individual spec or the context block containing multiple specs that need 
JavaScript enabled: 

```ruby

# For one spec
it 'presents information about abuse report', js: true do
    # assertions...
end

describe "Admin::AbuseReports", js: true do
   it 'presents information about abuse report' do
        # assertions...
    end
   it 'shows buttons for adding to abuse report' do
        # assertions...
    end
end
```
356

357 358
In Spinach, the JavaScript driver is enabled differently. In the `*.feature` 
file for the failing spec, add the @javascript flag above the Scenario: 
359

360 361 362 363 364 365 366 367 368
```
@javascript
Scenario: Developer can approve merge request
    Given I am a "Shop" developer
    And I visit project "Shop" merge requests page
    And merge request 'Bug NS-04' must be approved
    And I click link "Bug NS-04"
    When I click link "Approve"
    Then I should see approved merge request "Bug NS-04"
369

370
```