Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Substitute history #13

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ yarn add svelte-router
import Welcome from './Welcome.html'
import Animal from './Animal.html'

// import custom (browser) history
import history from './history'

const { createRouter, RouterLink } = SvelteRouter

const router = createRouter({
Expand All @@ -58,7 +61,7 @@ yarn add svelte-router
}
}
}
})
}, history)

export default {
oncreate () {
Expand Down
2 changes: 1 addition & 1 deletion build/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
<body>
<div id="example"></div>

<script src="./svelte-router.js"></script>
<script src="/svelte-router.js"></script>
</body>
</html>
5 changes: 4 additions & 1 deletion example/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import Hello from './Hello.html'
import NotFound from './NotFound.html'

// import custom (browser) history
import history from './history'

const { createRouter, RouterLink } = SvelteRouter

const store = new Store({
Expand All @@ -39,7 +42,7 @@
},
},
default: NotFound
})
}, history)
createRouter.listen(() => {
console.log('router changed')
})
Expand Down
2 changes: 1 addition & 1 deletion src/utils/history.js → example/history.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createHistory from 'history/createHashHistory'
import createHistory from 'history/createBrowserHistory'

const history = createHistory()

Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ if (process.env.NODE_ENV === 'production') {
config.input = './example/main.js'
config.plugins.unshift(
serve({
historyApiFallback: true,
contentBase: ['lib', 'build']
}),
livereload('release')
Expand Down
3 changes: 1 addition & 2 deletions src/components/RouterLink.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
</a>

<script>
import history from '../utils/history'
const activedClassName = 'router-link-active'

export default {
data () {
return {
replace: false,
to: '/',
basePath: '#',
basePath: '',
active: false,
class: '',
activeClass: activedClassName,
Expand Down
10 changes: 8 additions & 2 deletions src/utils/create-router.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import history from './history'
import createDefaultHistory from 'history/createHashHistory'

const DYNAMIC_PATH_REGEX = '[a-zA-Z]+'
const DEFAULT_ROUTE = 'default'
Expand Down Expand Up @@ -46,10 +46,16 @@ const getContent = (options, path, target, pathVariables) => {
})
}

const createRouter = options => {
const createRouter = (options, customHistory) => {
let _target // target DOM
let _unlisten // history listener
let _content // route instance
let history = customHistory || createDefaultHistory()

// define getter history proeprty
Object.defineProperty(this, 'history', {
get: () => history
})

const handleRouteChange = location => {
let found = false
Expand Down