Skip to content

Commit

Permalink
Reduce App Router bundle size (#1576)
Browse files Browse the repository at this point in the history
* Only use esm

* Use mjs imports instead of cjs

* Use mjs version of config

* Use `ApolloClient`/`InMemoryCache`

* Add changesets

* Use `isolatedModules`. Thanks @theodesp !
  • Loading branch information
blakewilson authored Sep 19, 2023
1 parent 55cf810 commit 77c5d4f
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 46 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-teachers-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/experimental-app-router': patch
---

Fixed an issue where the client was being shipped on the client resulting in large bundle sizes (150kb+). For more context: https://github.com/apollographql/apollo-client-nextjs/issues/95
5 changes: 5 additions & 0 deletions .changeset/silent-impalas-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/experimental-app-router': minor
---

**BREAKING**: Removed `cjs` support. The experimental app router package now is ESM only.
2 changes: 1 addition & 1 deletion examples/next/app-router/faust.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { setConfig } from '@faustwp/core/dist/cjs/config/index.js';
import { setConfig } from '@faustwp/core/dist/mjs/config/index.js';
import possibleTypes from './possibleTypes.json';

/** @type {import('@faustwp/core').FaustConfig} */
Expand Down
146 changes: 137 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/experimental-app-router/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ tests/**/*
.eslintignore
jest.config.js
jest.setup.ts
tsconfig-cjs.json
tsconfig.json
coverage
1 change: 1 addition & 0 deletions packages/experimental-app-router/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
'ts-jest',
{
tsconfig: 'tsconfig.json',
isolatedModules: true,
useESM: true,
},
],
Expand Down
13 changes: 6 additions & 7 deletions packages/experimental-app-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@
"name": "@faustwp/experimental-app-router",
"version": "0.0.4",
"description": "Experimental: A Faust package to support Next.js' App Router",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
"types": "dist/cjs/index.d.ts",
"exports": {
".": "./dist/index.js",
"./package.json": "./package.json"
},
"types": "dist/index.d.ts",
"scripts": {
"dev": "concurrently \"npm:watch-*\" --prefix-colors \"auto\"",
"build": "concurrently \"npm:build-*\" --prefix-colors \"auto\"",
"prebuild": "npm run clean",
"postbuild": "npm run package",
"clean": "rimraf dist",
"format": "prettier --write .",
"package": "node ../../scripts/package.js",
"prepublish": "npm run build",
"test:coverage:ci": "jest --ci --json --coverage --testLocationInResults --outputFile=report.json",
"test:coverage": "jest --coverage",
"test:watch": "jest --watch",
"test": "jest",
"build-js": "tsc -p .",
"build-cjs": "tsc -p tsconfig-cjs.json",
"watch-js": "concurrently \"npm:build-js -- --watch\" \"npm:build-cjs -- --watch\""
"watch-js": "concurrently \"npm:build-js -- --watch\""
},
"repository": {
"type": "git",
Expand Down
20 changes: 10 additions & 10 deletions packages/experimental-app-router/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { InMemoryCacheConfig, createHttpLink } from '@apollo/client';
import {
ApolloClient,
InMemoryCache,
InMemoryCacheConfig,
createHttpLink,
} from '@apollo/client';
// eslint-disable-next-line import/extensions
import { setContext } from '@apollo/client/link/context';
// eslint-disable-next-line import/extensions
import { registerApolloClient } from '@apollo/experimental-nextjs-app-support/rsc';
import {
NextSSRApolloClient,
NextSSRInMemoryCache,
// eslint-disable-next-line import/extensions
} from '@apollo/experimental-nextjs-app-support/ssr';
// eslint-disable-next-line import/extensions
/**
* We are currently importing these utils from their respective dist paths because importing
Expand All @@ -16,8 +16,8 @@ import {
*
* @todo Find a workaround for importing these utils without invoking FaustProvider.
*/
import { getConfig } from '@faustwp/core/dist/cjs/config/index.js';
import { getGraphqlEndpoint } from '@faustwp/core/dist/cjs/lib/getGraphqlEndpoint.js';
import { getConfig } from '@faustwp/core/dist/mjs/config/index.js';
import { getGraphqlEndpoint } from '@faustwp/core/dist/mjs/lib/getGraphqlEndpoint.js';
import { fetchAccessToken } from './server/auth/fetchAccessToken.js';

async function createFaustApolloClient(authenticated = false) {
Expand Down Expand Up @@ -65,8 +65,8 @@ async function createFaustApolloClient(authenticated = false) {
* we may set config differently than how we currently do it.
*/

return new NextSSRApolloClient({
cache: new NextSSRInMemoryCache(inMemoryCacheObject),
return new ApolloClient({
cache: new InMemoryCache(inMemoryCacheObject),
link: linkChain,
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { gql } from '@apollo/client';
// eslint-disable-next-line import/extensions
import { print } from '@apollo/client/utilities';
import { getGraphqlEndpoint } from '@faustwp/core/dist/cjs/lib/getGraphqlEndpoint.js';
import { isValidEmail } from '@faustwp/core/dist/cjs/utils/assert.js';
import { getGraphqlEndpoint } from '@faustwp/core/dist/mjs/lib/getGraphqlEndpoint.js';
import { isValidEmail } from '@faustwp/core/dist/mjs/utils/assert.js';
import { fetchTokens } from '../server/auth/fetchTokens.js';
import { setRefreshToken } from './utils/setRefreshToken.js';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cookies } from 'next/headers.js';
import { getWpUrl } from '@faustwp/core/dist/cjs/lib/getWpUrl.js';
import { getWpUrl } from '@faustwp/core/dist/mjs/lib/getWpUrl.js';

export async function onLogout() {
'use server';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getWpUrl } from '@faustwp/core/dist/cjs/lib/getWpUrl.js';
import { getWpUrl } from '@faustwp/core/dist/mjs/lib/getWpUrl.js';
import { cookies } from 'next/headers.js';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getWpUrl } from '@faustwp/core/dist/cjs/lib/getWpUrl.js';
import { getWpUrl } from '@faustwp/core/dist/mjs/lib/getWpUrl.js';
// eslint-disable-next-line import/extensions
import { cookies } from 'next/headers';
import { AuthorizeResponse } from '../routeHandler/tokenHandler.js';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getWpUrl } from '@faustwp/core/dist/cjs/lib/getWpUrl.js';
import { getWpSecret } from '@faustwp/core/dist/cjs/lib/getWpSecret.js';
import { getWpUrl } from '@faustwp/core/dist/mjs/lib/getWpUrl.js';
import { getWpSecret } from '@faustwp/core/dist/mjs/lib/getWpSecret.js';
import { cookies } from 'next/headers.js';
import { NextResponse } from 'next/server.js';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
onLogin,
validationError,
} from '../../src/server-actions/loginAction.js';
import { getGraphqlEndpoint } from '@faustwp/core/dist/cjs/lib/getGraphqlEndpoint.js';
import { getGraphqlEndpoint } from '@faustwp/core/dist/mjs/lib/getGraphqlEndpoint.js';
import * as fetchTokens from '../../src/server/auth/fetchTokens.js';
import * as setRefreshToken from '../../src/server-actions/utils/setRefreshToken.js';

Expand Down
9 changes: 0 additions & 9 deletions packages/experimental-app-router/tsconfig-cjs.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/experimental-app-router/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "esnext",
"outDir": "dist/mjs",
"outDir": "dist",
"target": "es2017",
"rootDir": "src",
"jsx": "react"
Expand Down

1 comment on commit 77c5d4f

@headless-platform-by-wp-engine

Choose a reason for hiding this comment

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

Check out the recent updates to your Atlas environment:

App Environment URL Build
faustjs canary https://hg…wered.com ✅ (logs)

Learn more about building on Atlas in our documentation.

Please sign in to comment.