Skip to content

Commit b398206

Browse files
committed
Set up host app for gh-pages history location support
1 parent b21f36f commit b398206

File tree

6 files changed

+46
-35
lines changed

6 files changed

+46
-35
lines changed

blueprints/ember-cli-addon-docs/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ module.exports = {
2323
afterInstall() {
2424
const configPath = require.resolve(this.project.configPath());
2525
const configContents = fs.readFileSync(configPath, 'utf-8')
26-
.replace(/rootURL: .*,/, `rootURL: '/${this.project.name()}/',`)
27-
.replace(/locationType: .*,/, `locationType: 'hash',`);
26+
.replace(/rootURL: .*,/, `rootURL: '/${this.project.name()}/',`);
2827

2928
fs.writeFileSync(configPath, configContents, 'utf-8');
30-
this.ui.writeInfoLine('Updated dummy app rootURL and locationType for compatibility with GitHub Pages.');
29+
this.ui.writeInfoLine('Updated dummy app rootURL for compatibility with GitHub Pages.');
3130
}
3231
};

index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-env node */
22
'use strict';
33

4+
const fs = require('fs');
45
const path = require('path');
56
const resolve = require('resolve');
67
const MergeTrees = require('broccoli-merge-trees');
@@ -77,6 +78,7 @@ module.exports = {
7778

7879
let importer = findImporter(this);
7980

81+
importer.import('vendor/404.html');
8082
importer.import(`${this._hasEmberSource() ? 'vendor' : 'bower_components'}/ember/ember-template-compiler.js`);
8183
importer.import('vendor/lunr/lunr.js', {
8284
using: [{ transformation: 'amd', as: 'lunr' }]
@@ -105,6 +107,12 @@ module.exports = {
105107
}
106108
},
107109

110+
contentFor(type) {
111+
if (type === 'body') {
112+
return fs.readFileSync(`${__dirname}/vendor/ember-cli-addon-docs/github-spa.html`, 'utf-8');
113+
}
114+
},
115+
108116
treeForVendor(vendor) {
109117
return new MergeTrees([
110118
vendor,
@@ -134,7 +142,11 @@ module.exports = {
134142
config: this.project.config(EmberApp.env())
135143
});
136144

137-
return new MergeTrees([ defaultTree, docsTree, searchIndexTree ]);
145+
let notFoundSnippet = new Funnel(`${__dirname}/vendor/ember-cli-addon-docs`, {
146+
include: ['404.html']
147+
});
148+
149+
return new MergeTrees([ defaultTree, notFoundSnippet, docsTree, searchIndexTree ]);
138150
},
139151

140152
_lunrTree() {

tests/dummy/app/index.html

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,37 +20,6 @@
2020
<body>
2121
{{content-for "body"}}
2222

23-
<!-- Start Single Page Apps for GitHub Pages -->
24-
<script type="text/javascript">
25-
// Single Page Apps for GitHub Pages
26-
// https://github.com/rafrex/spa-github-pages
27-
// Copyright (c) 2016 Rafael Pedicini, licensed under the MIT License
28-
// ----------------------------------------------------------------------
29-
// This script checks to see if a redirect is present in the query string
30-
// and converts it back into the correct url and adds it to the
31-
// browser's history using window.history.replaceState(...),
32-
// which won't cause the browser to attempt to load the new url.
33-
// When the single page app is loaded further down in this file,
34-
// the correct url will be waiting in the browser's history for
35-
// the single page app to route accordingly.
36-
(function(l) {
37-
if (l.search) {
38-
var q = {};
39-
l.search.slice(1).split('&').forEach(function(v) {
40-
var a = v.split('=');
41-
q[a[0]] = a.slice(1).join('=').replace(/~and~/g, '&');
42-
});
43-
if (q.p !== undefined) {
44-
window.history.replaceState(null, null,
45-
l.pathname.slice(0, -1) + (q.p || '') +
46-
(q.q ? ('?' + q.q) : '') +
47-
l.hash
48-
);
49-
}
50-
}
51-
}(window.location))
52-
</script>
53-
<!-- End Single Page Apps for GitHub Pages -->
5423
<script src="{{rootURL}}assets/vendor.js"></script>
5524
<script src="{{rootURL}}assets/dummy.js"></script>
5625

vendor/.gitkeep

Whitespace-only changes.
File renamed without changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<!-- Start Single Page Apps for GitHub Pages -->
2+
<script type="text/javascript">
3+
// Single Page Apps for GitHub Pages
4+
// https://github.com/rafrex/spa-github-pages
5+
// Copyright (c) 2016 Rafael Pedicini, licensed under the MIT License
6+
// ----------------------------------------------------------------------
7+
// This script checks to see if a redirect is present in the query string
8+
// and converts it back into the correct url and adds it to the
9+
// browser's history using window.history.replaceState(...),
10+
// which won't cause the browser to attempt to load the new url.
11+
// When the single page app is loaded further down in this file,
12+
// the correct url will be waiting in the browser's history for
13+
// the single page app to route accordingly.
14+
(function(l) {
15+
if (l.search) {
16+
var q = {};
17+
l.search.slice(1).split('&').forEach(function(v) {
18+
var a = v.split('=');
19+
q[a[0]] = a.slice(1).join('=').replace(/~and~/g, '&');
20+
});
21+
if (q.p !== undefined) {
22+
window.history.replaceState(null, null,
23+
l.pathname.slice(0, -1) + (q.p || '') +
24+
(q.q ? ('?' + q.q) : '') +
25+
l.hash
26+
);
27+
}
28+
}
29+
}(window.location))
30+
</script>
31+
<!-- End Single Page Apps for GitHub Pages -->

0 commit comments

Comments
 (0)