forked from dotnet/blazor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Live reloading (currently enabled only for command-line builds - will
add VS support next)
- Loading branch information
1 parent
512a15e
commit 0b3b84f
Showing
23 changed files
with
539 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Microsoft.AspNetCore.Blazor.Browser.JS/src/LiveReloading.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
export function enableLiveReloading(endpointUri: string) { | ||
listenForReloadEvent(endpointUri); | ||
} | ||
|
||
function listenForReloadEvent(endpointUri: string) { | ||
if (!WebSocket) { | ||
console.log('Browser does not support WebSocket, so live reloading will be disabled.'); | ||
return; | ||
} | ||
|
||
// First, connect to the endpoint | ||
const websocketUri = toAbsoluteWebSocketUri(endpointUri); | ||
const source = new WebSocket(websocketUri); | ||
let allowConnectionFailedErrorReporting = true; | ||
|
||
source.onopen = e => { | ||
allowConnectionFailedErrorReporting = false; | ||
}; | ||
|
||
source.onerror = e => { | ||
if (allowConnectionFailedErrorReporting) { | ||
allowConnectionFailedErrorReporting = false; | ||
console.error(`The client app was compiled with live reloading enabled, but could not open ` | ||
+ ` a WebSocket connection to the server at ${websocketUri}\n` | ||
+ `To fix this inconsistency, either run the server in development mode, or compile the ` | ||
+ `client app in Release configuration.`); | ||
} | ||
}; | ||
|
||
// If we're notified that we should reload, then do so | ||
source.onmessage = e => { | ||
if (e.data === 'reload') { | ||
location.reload(); | ||
} | ||
}; | ||
} | ||
|
||
function toAbsoluteWebSocketUri(uri: string) { | ||
const baseUri = document.baseURI; | ||
if (baseUri) { | ||
const lastSlashPos = baseUri.lastIndexOf('/'); | ||
const prefix = baseUri.substr(0, lastSlashPos); | ||
uri = prefix + uri; | ||
} | ||
|
||
// Scheme must be ws: or wss: | ||
return uri.replace(/^http/, 'ws'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.