Skip to content

Commit 137cf4d

Browse files
committed
hash urls and work at subdirectories
1 parent 8e2798e commit 137cf4d

File tree

9 files changed

+47
-10
lines changed

9 files changed

+47
-10
lines changed

config-overrides.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin
1+
const path = require('path')
2+
const HtmlWebpackPlugin = require('html-webpack-plugin')
3+
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
24

35
module.exports = function override (config, env) {
6+
// compile sass
7+
// https://github.com/facebookincubator/create-react-app/issues/2498
48
config.module.rules[1].oneOf.splice(config.module.rules[1].oneOf.length - 1, 0,
59
{
610
test: /\.scss$/,
@@ -17,8 +21,11 @@ module.exports = function override (config, env) {
1721
},
1822
)
1923
if (env === 'production') {
24+
// set the output filename to just socket.js
2025
config.output.filename = 'static/socket.js'
2126
config.output.chunkFilename = 'static/socket.[chunkhash:8].chunk.js'
27+
28+
// if run with `ANALYSE=1 yarn build` create report.js size report
2229
if (process.env.ANALYSE) {
2330
config.plugins.push(
2431
new BundleAnalyzerPlugin({
@@ -27,6 +34,15 @@ module.exports = function override (config, env) {
2734
})
2835
)
2936
}
37+
} else {
38+
// add another output file at localhost:3000/foobar/
39+
config.plugins.splice(2, 0,
40+
new HtmlWebpackPlugin({
41+
inject: true,
42+
template: path.resolve(__dirname, 'public', 'index.html'),
43+
filename: 'foobar/index.html'
44+
})
45+
)
3046
}
3147
// console.dir(config, { depth: 10, colors: true })
3248
return config

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"yarn": "1.3.x"
2929
},
3030
"dependencies": {
31+
"html-webpack-plugin": "^2.30.1",
3132
"marked": "^0.3.7",
3233
"node-sass": "^4.7.2",
3334
"raven-js": "^3.20.1",

public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
var api_root = null // 'http://localhost:8000'
3131
var socket1 = socket(public_key, {
3232
element: '#socket1',
33+
router_mode: 'history',
3334
api_root: api_root,
3435
mode: 'grid',
3536
event_callback: function (name, v) {

src/components/App.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class App extends Component {
1414
error: props.error,
1515
enquiry_form_info: null,
1616
}
17+
this.url_base = props.config.router_mode === 'history' ? props.config.url_root : '/'
18+
this.url = this.url.bind(this)
1719
this.get_text = this.get_text.bind(this)
1820

1921
this.get_enquiry = this.get_enquiry.bind(this)
@@ -26,6 +28,10 @@ class App extends Component {
2628
}
2729
}
2830

31+
url (url_) {
32+
return this.url_base + url_
33+
}
34+
2935
get_text (name, replacements) {
3036
let s = this.props.config.messages[name]
3137
for (let [k, v] of Object.entries(replacements || {})) {

src/components/contractors/Contractors.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class Contractors extends Component {
3030

3131
subject_change (selected_subject) {
3232
if (selected_subject) {
33-
this.props.history.push(`/subject/${slugify(selected_subject.name)}`)
33+
this.props.history.push(this.props.root.url(`subject/${slugify(selected_subject.name)}`))
3434
} else {
35-
this.props.history.push(`/`)
35+
this.props.history.push(this.props.root.url(''))
3636
}
3737
this.update_contractors(selected_subject)
3838
}
@@ -77,9 +77,9 @@ class Contractors extends Component {
7777
subjects={this.state.subjects}
7878
selected_subject={this.state.selected_subject}
7979
subject_change={this.subject_change}
80-
get_text={this.props.root.get_text}/>
80+
root={this.props.root}/>
8181

82-
<Route path="/:id([0-9]+):_extra" render={props => (
82+
<Route path={this.props.root.url(':id([0-9]+):_extra')} render={props => (
8383
<ConModal id={props.match.params.id}
8484
contractors={this.state.contractors}
8585
got_contractors={this.state.got_contractors}

src/components/contractors/Grid.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const Grid = props => {
88
let description = ''
99
if (props.selected_subject) {
1010
const msg_id_suffix = props.contractors.length === 1 ? 'single' : 'plural'
11-
description = props.get_text('subject_filter_summary_' + msg_id_suffix, {
11+
description = props.root.get_text('subject_filter_summary_' + msg_id_suffix, {
1212
count: props.contractors.length,
1313
subject: props.selected_subject.name,
1414
})
@@ -32,7 +32,7 @@ const Grid = props => {
3232
<div className="tcs-flex">
3333
{props.contractors.map((contractor, i) => (
3434
<div key={i} className="tcs-col">
35-
<Link to={`/${contractor.link}`} className="tcs-box">
35+
<Link to={props.root.url(contractor.link)} className="tcs-box">
3636
<img src={contractor.photo} alt={contractor.name} className="tcs-thumb"/>
3737
<h3 className="tcs-name">{contractor.name}</h3>
3838
</Link>

src/components/enquiry/EnquiryButton.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import EnquiryModal from './EnquiryModal'
44

55
const EnquiryButton = ({root, config}) => (
66
<div className="tcs-app">
7-
<Link to="/enquiry" className="tcs-enquiry-button">
7+
<Link to={root.url('enquiry')} className="tcs-enquiry-button">
88
{root.get_text('enquiry_button')}
99
</Link>
1010

11-
<Route path="/enquiry" render={props => (
11+
<Route path={root.url('enquiry')} render={props => (
1212
<EnquiryModal root={root} config={config} history={props.history}/>
1313
)}/>
1414
</div>

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react'
33
import ReactDOM from 'react-dom'
44
import './main.scss'
55
import App from './components/App'
6-
import {BrowserRouter as Router} from 'react-router-dom'
6+
import {BrowserRouter, HashRouter} from 'react-router-dom'
77
import {auto_url_root} from './utils'
88

99
const raven_config = {
@@ -122,6 +122,8 @@ window.socket = function (public_key, config) {
122122

123123
console.debug('using config:', config)
124124

125+
const Router = config.router_mode === 'history' ? BrowserRouter : HashRouter
126+
125127
const v = ReactDOM.render(<Router><App error={error} public_key={public_key} config={config}/></Router>, el)
126128
// TODO provide a better object here?
127129

yarn.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,6 +3113,17 @@ [email protected]:
31133113
pretty-error "^2.0.2"
31143114
toposort "^1.0.0"
31153115

3116+
html-webpack-plugin@^2.30.1:
3117+
version "2.30.1"
3118+
resolved "https://registry.yarnpkg.com/html-webpack-plugin/-/html-webpack-plugin-2.30.1.tgz#7f9c421b7ea91ec460f56527d78df484ee7537d5"
3119+
dependencies:
3120+
bluebird "^3.4.7"
3121+
html-minifier "^3.2.3"
3122+
loader-utils "^0.2.16"
3123+
lodash "^4.17.3"
3124+
pretty-error "^2.0.2"
3125+
toposort "^1.0.0"
3126+
31163127
htmlparser2@~3.3.0:
31173128
version "3.3.0"
31183129
resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-3.3.0.tgz#cc70d05a59f6542e43f0e685c982e14c924a9efe"

0 commit comments

Comments
 (0)