Skip to content

Commit

Permalink
fix: hmr not working with custom publicPath that has a different prot…
Browse files Browse the repository at this point in the history
…ocol
  • Loading branch information
liximomo committed Feb 9, 2023
1 parent bc47b78 commit fc8d4f8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions packages/platform-shared/src/shared/helper/getAppData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function getAppData(): IAppData {
return {
ssr: false,
filesByRoutId: {},
publicPath: ''
publicPath: '/'
};
}

Expand All @@ -39,7 +39,7 @@ export function getAppData(): IAppData {
ssr: false,
pageData: {},
filesByRoutId: {},
publicPath: ''
publicPath: '/'
};
}

Expand Down
2 changes: 1 addition & 1 deletion packages/platform-shared/src/shared/loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function redirectHelper(to: string, status: number = 302) {
if (process.env.NODE_ENV === 'development') {
invariant(
typeof to === 'string',
`redirect fist argument should be string, now is ${typeof to}`
`redirect's frist argument should be string, now is ${typeof to}`
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/platform-web/src/shuvi-app/dev/hotDevClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export type HotDevClient = {
export default function connect(options: {
launchEditorEndpoint: string;
path: string;
location: Location;
assetPublicPath: string;
}): HotDevClient {
startReportingRuntimeErrors({
onError: function () {
Expand Down
3 changes: 2 additions & 1 deletion packages/platform-web/src/shuvi-app/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DEV_HOT_MIDDLEWARE_PATH,
DEV_HOT_LAUNCH_EDITOR_ENDPOINT
} from '@shuvi/shared/constants';
import { getPublicPath } from '@shuvi/platform-shared/shared/helper/getPublicPath';
import { InternalApplication } from '../../shared';
import connect, { HotDevClient } from './hotDevClient';

Expand All @@ -21,7 +22,7 @@ export const initHMRAndDevClient = (app: InternalApplication) => {
devClient = connect({
launchEditorEndpoint: DEV_HOT_LAUNCH_EDITOR_ENDPOINT,
path: DEV_HOT_MIDDLEWARE_PATH,
location
assetPublicPath: getPublicPath()
});

setInterval(() => {
Expand Down
30 changes: 21 additions & 9 deletions packages/platform-web/src/shuvi-app/dev/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ const eventCallbacks: ((event: any) => void)[] = [];
let lastActivity = Date.now();
let initDataBeforeWsOnline: any[] = [];

function getSocketProtocol(protocol: string): string {
function getSocketProtocol(assetPublicPath: string): string {
let protocol = window.location.protocol;

try {
// assetPublicPath is a url
protocol = new URL(assetPublicPath).protocol;
} catch (_) {}

return protocol === 'http:' ? 'ws' : 'wss';
}

Expand All @@ -28,11 +35,7 @@ export function connectHMR(options: {
path: string;
timeout: number;
log?: boolean;
location: {
protocol: string;
hostname: string;
port?: string;
};
assetPublicPath: string;
}) {
if (!options.timeout) {
options.timeout = 5000;
Expand All @@ -48,9 +51,18 @@ export function connectHMR(options: {

function init() {
if (source) source.close();
let { protocol, hostname, port } = options.location;
protocol = getSocketProtocol(protocol);
let url = `${protocol}://${hostname}:${port}`;

const { hostname, port } = window.location;
const protocol = getSocketProtocol(options.assetPublicPath);
const assetPublicPath = options.assetPublicPath.replace(/^\/+/, '');

let url = `${protocol}://${hostname}:${port}${
assetPublicPath ? `/${assetPublicPath}` : ''
}`;

if (assetPublicPath.startsWith('http')) {
url = `${protocol}://${assetPublicPath.split('://')[1]}`;
}

source = new WebSocket(`${url}${options.path}`);
source.onopen = handleOnline;
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/css.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { DEV_STYLE_HIDE_FOUC } from '@shuvi/shared/constants';
import { AppCtx, Page, devFixture } from '../utils';

let ctx: AppCtx;
let page: Page;

jest.setTimeout(5 * 60 * 1000);

['CSS', 'LightningCss'].forEach(function (describeName) {
let ctx: AppCtx;
let page: Page;

describe(describeName, () => {
beforeAll(async () => {
ctx = await devFixture(
Expand Down

0 comments on commit fc8d4f8

Please sign in to comment.