Skip to content

Commit 6a85fb9

Browse files
committed
Added navigate()
1 parent fffb854 commit 6a85fb9

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed

dev/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ const LiveServer = require('../lib/index').default
22

33
const liveServer = new LiveServer()
44

5-
liveServer.start({ noBrowser: true })
5+
liveServer.start({ open: false })

injected.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ if ('WebSocket' in window) {
7373
else if (msg.data === 'initRemoteLogs') overwriteLogs()
7474
else {
7575
const d = JSON.parse(msg.data)
76+
if (d.navigate) window.location.replace(d.navigate)
7677
if (d.body) injectBody(d.body)
7778
if (d.position) {
7879
// TODO: This highlight section needs improvement

src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,16 @@ export default class LiveServer {
456456
})
457457
}
458458

459+
/**
460+
* Navigate the browser to another page.
461+
* @param url Navigates to the given URL.
462+
*/
463+
public navigate(url: string) {
464+
this.clients.forEach(ws => {
465+
if (ws) ws.sendWithDelay(JSON.stringify({ navigate: url }))
466+
})
467+
}
468+
459469
/** Launch a new browser window. */
460470
public async launchBrowser(
461471
path: string | boolean | string[] | null | undefined,

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ export interface LiveServerParams {
5555
highlight?: boolean
5656
/** Set to false to not inject body changes. (VSCode Extension only) */
5757
injectBody?: boolean
58-
/** Absolute path of your vscode workspace. (VSCode Extension only) */
58+
/** Navigates your browser automatically to the current working file. (VSCode Extension only) */
59+
navigate?: boolean
60+
/** @private Absolute path of your workspace. (VSCode Extension only) */
5961
workspace?: string
6062

6163
/** @deprecated No need for an external https module */

test/navigation.test.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const path = require('path')
2+
const FiveServer = require('../lib').default
3+
const puppeteer = require('puppeteer')
4+
5+
jest.setTimeout(15_000)
6+
7+
const fiveServer = new FiveServer()
8+
let browser
9+
let page
10+
11+
const options = {
12+
root: path.join(__dirname, 'data'),
13+
port: 40200,
14+
open: false
15+
}
16+
17+
const pause = (ms = 1000) => {
18+
return new Promise(resolve => {
19+
setTimeout(() => {
20+
resolve()
21+
}, ms)
22+
})
23+
}
24+
25+
const getInnerText = async query => {
26+
const el = await page.$(query)
27+
28+
const innerText = await page.evaluate(x => {
29+
return x.innerText
30+
}, el)
31+
32+
return innerText
33+
}
34+
35+
beforeAll(async () => {
36+
await fiveServer.start(options)
37+
38+
browser = await puppeteer.launch()
39+
40+
page = await browser.newPage()
41+
page.on('console', msg => {
42+
log = msg.text()
43+
})
44+
45+
await page.goto('http://localhost:40200/', { waitUntil: 'networkidle2' })
46+
})
47+
48+
describe('navigation test', () => {
49+
it('should open index.html at /', async done => {
50+
const h1 = await getInnerText('h1')
51+
expect(page.url()).toBe('http://localhost:40200/')
52+
expect(h1).toBe('Hello world.')
53+
54+
done()
55+
})
56+
57+
it('should navigate to /sub/sub.html', async done => {
58+
await fiveServer.navigate('/sub/sub.html')
59+
await pause()
60+
61+
const h1 = await getInnerText('h1')
62+
expect(page.url()).toBe('http://localhost:40200/sub/sub.html')
63+
expect(h1).toBe('Subdirectory')
64+
65+
done()
66+
})
67+
})
68+
69+
afterAll(async () => {
70+
await fiveServer.shutdown()
71+
await browser.close()
72+
})

0 commit comments

Comments
 (0)