Skip to content

Commit

Permalink
refactor: prefetch modules, fix windows build
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley authored May 17, 2022
1 parent b4def22 commit a30f18a
Show file tree
Hide file tree
Showing 79 changed files with 2,235 additions and 1,438 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ jobs:

- name: Validate Create Qwik Cli
if: ${{ needs.changes.outputs.fullbuild == 'true' }}
run: yarn node scripts/validate-cli.cjs
run: yarn cli.validate

############ UNIT TEST ############
test-unit:
Expand Down
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ etc
temp
tsdoc-metadata.json
**/.DS_Store
integration/out/
integration/todo/output
integration/*.js
integration/*.cjs
integration/*.map
starters/**/server
src/napi/package-*
target
*.node
Expand Down
1 change: 0 additions & 1 deletion .husky/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion .husky/pre-commit

This file was deleted.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
"name": "qwik-monorepo",
"version": "0.0.20-3",
"scripts": {
"build": "yarn node scripts --tsc --build --api --eslint --platform-binding --wasm",
"build": "yarn node scripts --tsc --build --platform-binding-wasm-copy",
"build.full": "yarn node scripts --tsc --build --api --eslint --platform-binding --wasm",
"build.platform": "yarn node scripts --platform-binding",
"build.platform.copy": "yarn node scripts --platform-binding-wasm-copy",
"build.only_javascript": "yarn node scripts --tsc --build --api",
"build.wasm": "yarn node scripts --wasm",
"build.validate": "yarn node scripts --tsc --build --api --eslint --platform-binding --wasm --validate",
"build.cli": "yarn node scripts --cli --dev",
"build.cli.prod": "yarn node scripts --cli",
"cli": "yarn build.cli && node packages/create-qwik/dist/create-qwik",
"cli.validate": "yarn node -r esbuild-register scripts/validate-cli.ts",
"build.watch": "yarn node scripts --build --watch --dev --platform-binding",
"api.update": "yarn node scripts --tsc --api --dev",
"tsc.check": "tsc --noEmit",
Expand All @@ -30,7 +33,6 @@
"serve": "yarn node -r esbuild-register --inspect starters/dev-server.ts 3300",
"docs.fetch.hackMD": "yarn node --trace-warnings -r esbuild-register scripts/docs_sync/fetch_hackmd.ts",
"docs.sync": "yarn node scripts/docs_sync",
"prepare": "husky install",
"lint": "yarn lint.eslint && yarn lint.prettier && yarn lint.rust",
"lint.eslint": "eslint \"**/*.ts*\"",
"lint.rust": "make lint",
Expand All @@ -43,7 +45,7 @@
"devDependencies": {
"@builder.io/partytown": "0.5.4",
"@builder.io/qwik-dom": "2.1.14",
"@microsoft/api-extractor": "7.23.1",
"@microsoft/api-extractor": "7.23.2",
"@napi-rs/cli": "2.6.2",
"@napi-rs/triples": "1.1.0",
"@node-rs/helper": "1.3.3",
Expand All @@ -55,6 +57,7 @@
"@types/jest": "27.5.0",
"@types/mri": "1.1.1",
"@types/node": "17.0.31",
"@types/node-fetch": "2.6.1",
"@types/path-browserify": "1.0.0",
"@types/semver": "7.3.9",
"@types/source-map-support": "0.5.4",
Expand All @@ -70,11 +73,10 @@
"execa": "6.1.0",
"express": "4.18.1",
"gzip-size": "6.0.0",
"husky": "7.0.4",
"jest": "27.5.1",
"lint-staged": "12.4.1",
"mri": "1.2.0",
"node-fetch": "^2.0.0",
"node-fetch": "2.6.7",
"path-browserify": "1.0.1",
"prettier": "2.6.2",
"rollup": "2.72.1",
Expand Down Expand Up @@ -138,7 +140,6 @@
},
"modulePathIgnorePatterns": [
"<rootDir>/.github/",
"<rootDir>/.husky/",
"<rootDir>/.vscode/",
"<rootDir>/packages/eslint-plugin-qwik/dist/",
"<rootDir>/packages/qwik/dist/",
Expand All @@ -156,7 +157,6 @@
],
"testPathIgnorePatterns": [
"<rootDir>/.github/",
"<rootDir>/.husky/",
"<rootDir>/.vscode/",
"<rootDir>/bin/",
"<rootDir>/packages/create-qwik/",
Expand Down
33 changes: 29 additions & 4 deletions packages/docs/.gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
# Build
build
dist
server
functions/**/*.js

# Development
node_modules

# Cache
.cache
.mf
.vscode
.rollup.cache
server
dist
node_modules
tsconfig.tsbuildinfo
q-symbols.json

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

# Editor
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
75 changes: 3 additions & 72 deletions packages/docs/functions/[[path]].ts
Original file line number Diff line number Diff line change
@@ -1,74 +1,5 @@
/* eslint-disable */

// @ts-ignore
import { render } from '../server/entry.server.js';
import type { RenderToStringOptions, RenderToStringResult } from '@builder.io/qwik/server';

// @ts-ignore
import manifest from '../dist/q-manifest.json';

export const onRequestGet: PagesFunction = async ({ request, next, waitUntil }) => {
try {
const url = new URL(request.url);
if (url.hostname === 'qwik.builder.io' && url.pathname === '/') {
// temporarily redirect homepage to the overview page
return Response.redirect('https://qwik.builder.io/docs/overview', 302);
}

if (url.pathname === '/chat') {
return Response.redirect('https://discord.gg/bNVSQmPzqy');
}

// Handle static assets
if (/\.\w+$/.test(url.pathname) || url.pathname === '/repl/') {
return next(request);
}

// do not using caching during development
const useCache = url.hostname !== 'localhost';

// Early return from cache
const cache = await caches.open('custom:qwik');
if (useCache) {
const cachedResponse = await cache.match(request);
if (cachedResponse) {
return cachedResponse;
}
}

// Render To String Options
const opts: RenderToStringOptions = {
url: request.url,
base: '/build/',
manifest: manifest as any,
prefetchStrategy: {
symbolsToPrefetch: 'events-document',
implementation: 'link-prefetch',
},
};

// Generate Qwik SSR response
const result: RenderToStringResult = await render(opts);

const response = new Response(result.html, {
headers: {
'Cross-Origin-Embedder-Policy': 'credentialless',
'Cross-Origin-Opener-Policy': 'same-origin',
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': useCache
? `max-age=60, s-maxage=10, stale-while-revalidate=604800, stale-if-error=604800`
: `no-cache, max-age=0`,
},
});

if (useCache) {
waitUntil(cache.put(request, response.clone()));
}

// Return Qwik SSR response
return response;
} catch (e: any) {
// 500 Error
return new Response(String(e.stack || e), { status: 500 });
}
};
// Cloudflare Pages Functions
// https://developers.cloudflare.com/pages/platform/functions/
export { onRequestGet } from '../server/entry.cloudflare.js';
4 changes: 2 additions & 2 deletions packages/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build": "yarn build.client && yarn build.ssr",
"build.client": "vite build",
"build.ssr": "vite build --mode ssr",
"build.ssr": "vite build --ssr src/entry.cloudflare.tsx",
"build.qwikcity": "cd ../qwik-city && yarn build",
"dev": "vite --force",
"dev.ssr": "vite --mode ssr",
Expand All @@ -19,7 +19,7 @@
},
"devDependencies": {
"@builder.io/partytown": "^0.5.4",
"@builder.io/qwik": "workspace:*",
"@builder.io/qwik": "0.0.20-3",
"@builder.io/qwik-city": "0.0.4",
"@cloudflare/kv-asset-handler": "0.2.0",
"@cloudflare/workers-types": "^3.9.0",
Expand Down
8 changes: 5 additions & 3 deletions packages/docs/src/components/repl/worker/ssr-html.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/* eslint-disable no-console */

import type { RenderToStringOptions, RenderToStringResult } from '@builder.io/qwik/server';
import type { ReplInputOptions, ReplResult, ReplResultAttributes } from '../types';
import type { QwikWorkerGlobal } from './repl-service-worker';

export const ssrHtml = async (options: ReplInputOptions, result: ReplResult) => {
const ssrModule = result.ssrModules.find((m) => m.path.endsWith('.js'));
if (!ssrModule) {
if (!ssrModule || typeof ssrModule.code !== 'string') {
return;
}

Expand All @@ -17,13 +16,16 @@ export const ssrHtml = async (options: ReplInputOptions, result: ReplResult) =>
run(mod, mod.exports, noopRequire);

const server: ServerModule = mod.exports;
if (typeof server.render !== 'function') {
return;
}

const ssrResult = await server.render({
base: '/repl/',
manifest: result.manifest,
});

const doc = self.qwikServer.createDocument({ html: ssrResult.html });
const doc = self.qwikServer._createDocument({ html: ssrResult.html });
const qwikLoader = doc.getElementById('qwikloader');
if (qwikLoader) {
qwikLoader.remove();
Expand Down
62 changes: 62 additions & 0 deletions packages/docs/src/entry.cloudflare.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { render } from './entry.ssr';

export const onRequestGet: PagesFunction = async ({ request, next, waitUntil }) => {
try {
const url = new URL(request.url);
if (url.hostname === 'qwik.builder.io' && url.pathname === '/') {
// temporarily redirect homepage to the overview page
return Response.redirect('https://qwik.builder.io/docs/overview', 302);
}

if (url.pathname === '/chat') {
return Response.redirect('https://discord.gg/bNVSQmPzqy');
}

// Handle static assets
if (/\.\w+$/.test(url.pathname) || url.pathname === '/repl/') {
return next(request);
}

// do not using caching during development
const useCache = url.hostname !== 'localhost';

// Early return from cache
const cache = await caches.open('custom:qwik');
if (useCache) {
const cachedResponse = await cache.match(request);
if (cachedResponse) {
return cachedResponse;
}
}

// Generate Qwik SSR response
const result = await render({
url: request.url,
prefetchStrategy: {
symbolsToPrefetch: 'events-document',
implementation: 'link-prefetch',
},
});

const response = new Response(result.html, {
headers: {
'Cross-Origin-Embedder-Policy': 'credentialless',
'Cross-Origin-Opener-Policy': 'same-origin',
'Content-Type': 'text/html; charset=utf-8',
'Cache-Control': useCache
? `max-age=60, s-maxage=10, stale-while-revalidate=604800, stale-if-error=604800`
: `no-cache, max-age=0`,
},
});

if (useCache) {
waitUntil(cache.put(request, response.clone()));
}

// Return Qwik SSR response
return response;
} catch (e: any) {
// 500 Error
return new Response(String(e.stack || e), { status: 500 });
}
};
6 changes: 0 additions & 6 deletions packages/docs/src/entry.server.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { renderToString, RenderToStringOptions } from '@builder.io/qwik/server';
// import { manifest } from '@qwik-client-manifest';
import { Root } from './root';

/**
* Qwik server-side render function.
*/
export function render(opts: RenderToStringOptions) {
return renderToString(<Root />, opts);
return renderToString(<Root />, { ...opts });
}
22 changes: 14 additions & 8 deletions packages/docs/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ export default defineConfig(() => {
const pagesDir = resolve('pages');

return {
build: {
rollupOptions: {
input: [
resolve('src', 'components', 'app', 'app.tsx'),
resolve('src', 'components', 'repl', 'worker', 'repl-service-worker.ts'),
],
},
ssr: {
// SSR builds for the edge should use the "webworker" target
target: 'webworker',

// No external imports in ssr module, instead bundle into one file
noExternal: true,
},
plugins: [
qwikCity({
Expand All @@ -30,7 +29,14 @@ export default defineConfig(() => {
default: resolve('src', 'layouts', 'docs', 'docs.tsx'),
},
}),
qwikVite(),
qwikVite({
client: {
input: [
resolve('src', 'components', 'app', 'app.tsx'),
resolve('src', 'components', 'repl', 'worker', 'repl-service-worker.ts'),
],
},
}),
partytownVite({
dest: resolve('dist', '~partytown'),
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/qwik-city/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@types/mdx": "2.0.1"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.23.1",
"@microsoft/api-extractor": "7.23.2",
"@types/github-slugger": "^1.3.0",
"@types/marked": "^4.0.3",
"@types/refractor": "^3.0.2",
Expand Down
Loading

0 comments on commit a30f18a

Please sign in to comment.