Skip to content

Commit

Permalink
change load file result from file name to contentType
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Nov 6, 2020
1 parent d20c655 commit 803e67c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 29 deletions.
2 changes: 1 addition & 1 deletion plugins/web-test-runner-plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ To Resolve:
}
const reqPath = request.path;
const result = await server.loadUrl(reqPath, {isSSR: false, encoding: 'utf8'});
return result.contents;
return {body: result.contents, type: result.contentType};
},
transformImport({source}) {
if (!isTestFilePath(source) || source.startsWith('/__web-dev-server')) {
Expand Down
35 changes: 9 additions & 26 deletions snowpack/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,10 @@ function shouldProxy(pathPrefix: string, reqUrl: string) {
function sendResponseFile(
req: http.IncomingMessage,
res: http.ServerResponse,
{contents, originalFileLoc, responseFileName}: LoadResult,
{contents, originalFileLoc, contentType}: LoadResult,
) {
const body = Buffer.from(contents);
const ETag = etag(body, {weak: true});
const contentType = mime.contentType(responseFileName);
const headers: Record<string, string> = {
'Accept-Ranges': 'bytes',
'Access-Control-Allow-Origin': '*',
Expand Down Expand Up @@ -515,21 +514,21 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
return {
contents: encodeResponse(HMR_CLIENT_CODE, encoding),
originalFileLoc: null,
responseFileName: 'hmr-client.js',
contentType: 'application/javascript',
};
}
if (reqPath === getMetaUrlPath('/hmr-error-overlay.js', config)) {
return {
contents: encodeResponse(HMR_OVERLAY_CODE, encoding),
originalFileLoc: null,
responseFileName: 'hmr-error-overlay.js',
contentType: 'application/javascript',
};
}
if (reqPath === getMetaUrlPath('/env.js', config)) {
return {
contents: encodeResponse(generateEnvModule({mode: 'development', isSSR}), encoding),
originalFileLoc: null,
responseFileName: 'env.js',
contentType: 'application/javascript',
};
}

Expand Down Expand Up @@ -936,11 +935,7 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
return {
contents: encodeResponse(responseContent, encoding),
originalFileLoc: fileLoc,
responseFileName: replaceExt(
path.basename(fileLoc),
path.extname(fileLoc),
responseFileExt,
),
contentType: mime.lookup(responseFileExt),
};
}

Expand All @@ -953,11 +948,7 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
return {
contents: encodeResponse(fileContents, encoding),
originalFileLoc: fileLoc,
responseFileName: replaceExt(
path.basename(fileLoc),
path.extname(fileLoc),
responseFileExt,
),
contentType: mime.lookup(responseFileExt),
};
}
// Otherwise, finalize the response (where resolution happens) before sending.
Expand All @@ -983,11 +974,7 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
return {
contents: encodeResponse(responseContent, encoding),
originalFileLoc: fileLoc,
responseFileName: replaceExt(
path.basename(fileLoc),
path.extname(fileLoc),
responseFileExt,
),
contentType: mime.lookup(responseFileExt),
};
}

Expand Down Expand Up @@ -1035,11 +1022,7 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
return {
contents: encodeResponse(wrappedResponse, encoding),
originalFileLoc: fileLoc,
responseFileName: replaceExt(
path.basename(fileLoc),
path.extname(fileLoc),
responseFileExt,
),
contentType: mime.lookup(responseFileExt),
// ...but verify.
checkStale: async () => {
let checkFinalBuildResult: SnowpackBuildMap | null = null;
Expand Down Expand Up @@ -1114,7 +1097,7 @@ export async function startDevServer(commandOptions: CommandOptions): Promise<Sn
return {
contents: encodeResponse(responseContent, encoding),
originalFileLoc: fileLoc,
responseFileName: replaceExt(path.basename(fileLoc), path.extname(fileLoc), responseFileExt),
contentType: mime.lookup(responseFileExt),
};
}

Expand Down
4 changes: 2 additions & 2 deletions snowpack/src/types/snowpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type DeepPartial<T> = {
export interface LoadResult<T = Buffer | string> {
contents: T;
originalFileLoc: string | null;
responseFileName: string;
contentType: string | false;
checkStale?: () => Promise<void>;
}

Expand Down Expand Up @@ -57,7 +57,7 @@ export interface SnowpackDevServer {
sendResponseFile: (
req: http.IncomingMessage,
res: http.ServerResponse,
{contents, originalFileLoc, responseFileName}: LoadResult,
{contents, originalFileLoc, contentType}: LoadResult,
) => void;
sendResponseError: (req: http.IncomingMessage, res: http.ServerResponse, status: number) => void;
onFileChange: (callback: OnFileChangeCallback) => void;
Expand Down

1 comment on commit 803e67c

@vercel
Copy link

@vercel vercel bot commented on 803e67c Nov 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.