Skip to content

Commit

Permalink
fix(graphql): hot reload page-query changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hjvedvik committed Sep 28, 2018
1 parent cff46b9 commit 017aa95
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
9 changes: 7 additions & 2 deletions gridsome/__tests__/Source.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ test('generate slug from any string', () => {
test('add page', () => {
const emit = jest.spyOn(source, 'emit')
const page = source.addPage('page', {
title: 'Lorem ipsum dolor sit amet'
title: 'Lorem ipsum dolor sit amet',
internal: { origin: 'Test.vue' }
})

expect(page).toHaveProperty('$loki')
Expand All @@ -155,6 +156,7 @@ test('add page', () => {
expect(page.title).toEqual('Lorem ipsum dolor sit amet')
expect(page.slug).toEqual('lorem-ipsum-dolor-sit-amet')
expect(page.path).toEqual('/lorem-ipsum-dolor-sit-amet')
expect(page.internal.origin).toEqual('Test.vue')
expect(emit).toHaveBeenCalledTimes(1)

emit.mockRestore()
Expand Down Expand Up @@ -182,17 +184,20 @@ test('add page with query', () => {
test('update page', () => {
source.addPage('page', {
_id: 'test',
title: 'Lorem ipsum dolor sit amet'
title: 'Lorem ipsum dolor sit amet',
internal: { origin: 'Test.vue' }
})

const emit = jest.spyOn(source, 'emit')
const page = source.updatePage('test', {
internal: { origin: 'Test2.vue' },
title: 'New title'
})

expect(page.title).toEqual('New title')
expect(page.slug).toEqual('lorem-ipsum-dolor-sit-amet')
expect(page.path).toEqual('/lorem-ipsum-dolor-sit-amet')
expect(page.internal.origin).toEqual('Test2.vue')
expect(emit).toHaveBeenCalledTimes(1)

emit.mockRestore()
Expand Down
4 changes: 3 additions & 1 deletion gridsome/lib/plugins/source-vue/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ function createPage (source, file) {
pageQuery,
slug: source.source.slugify(name),
path: createPagePath(file),
file
internal: {
origin: file
}
}

if (/^src\/pages\/404\.vue$/.test(file)) {
Expand Down
2 changes: 1 addition & 1 deletion gridsome/lib/utils/Plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class Plugins extends EventEmitter {
this.emit('broadcast', {
type: 'updateQuery',
query: page.pageQuery.content,
file: page.file
file: page.internal.origin
})
})
}
Expand Down
6 changes: 5 additions & 1 deletion gridsome/lib/utils/Source.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ class Source extends EventEmitter {
_id: options._id,
type: type || 'page',
component: options.component,
internal: this.createInternals({ type })
internal: this.createInternals(options.internal)
}

try {
Expand All @@ -153,6 +153,7 @@ class Source extends EventEmitter {
page.title = options.title || page._id
page.slug = options.slug || this.slugify(page.title)
page.path = options.path || `/${page.slug}`
page.file = options.file

this.emit('addPage', page)

Expand All @@ -162,6 +163,7 @@ class Source extends EventEmitter {
updatePage (id, options) {
const page = this.getPage(id)
const oldPage = cloneDeep(page)
const internal = this.createInternals(options.internal)

try {
page.pageQuery = options.pageQuery
Expand All @@ -172,6 +174,8 @@ class Source extends EventEmitter {
page.title = options.title || page.title
page.slug = options.slug || page.slug
page.path = options.path || `/${page.slug}`
page.file = options.file || page.file
page.internal = Object.assign({}, page.internal, internal)

this.emit('updatePage', page, oldPage)

Expand Down

0 comments on commit 017aa95

Please sign in to comment.