Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/support pullfresh #6121

Merged
merged 3 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/smooth-sloths-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/plugin-pha': patch
---

feat: support pull refresh
6 changes: 6 additions & 0 deletions packages/plugin-pha/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,15 @@ export const validPageConfigKeys = [
'spm',
'queryParams',
'queryParamsPassKeys',
'pullRefresh',
'queryParamsPassIgnoreKeys',
];

// The manifest configuration is the default value for the page configuration
export const pageDefaultValueKeys = [
'pullRefresh',
];

export const getCompilerConfig = (options: {
getAllPlugin: Context['getAllPlugin'];
}) => {
Expand Down
19 changes: 17 additions & 2 deletions packages/plugin-pha/src/manifestHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import consola from 'consola';
import cloneDeep from 'lodash.clonedeep';
import { matchRoutes } from '@remix-run/router';
import * as htmlparser2 from 'htmlparser2';
import { decamelizeKeys, camelizeKeys, validPageConfigKeys } from './constants.js';
import { decamelizeKeys, camelizeKeys, validPageConfigKeys, pageDefaultValueKeys } from './constants.js';
import type { Page, PageHeader, PageConfig, Manifest, PHAManifest, Frame } from './types';

const { decamelize } = humps;
Expand Down Expand Up @@ -51,7 +51,7 @@ interface InternalPageConfig {
type MixedPage = InternalPageConfig & PageConfig;

export function transformManifestKeys(manifest: Manifest, options?: TransformOptions): PHAManifest {
const { parentKey, isRoot } = options;
const { parentKey, isRoot } = options || {};
const data = {};

for (let key in manifest) {
Expand Down Expand Up @@ -93,6 +93,13 @@ export function transformManifestKeys(manifest: Manifest, options?: TransformOpt
}
return item;
});
} else if (key === 'pullRefresh') {
if (value && value.reload) {
// Need reload.
data['pull_refresh'] = true;
} else {
data['enable_pull_refresh'] = true;
}
} else if (key === 'requestHeaders') {
// Keys of requestHeaders should not be transformed.
data[transformKey] = value;
Expand Down Expand Up @@ -346,6 +353,14 @@ export async function parseManifest(manifest: Manifest, options: ParseOptions):
manifest.pages = await Promise.all(routes.map(async (page) => {
const pageIds = getRouteIdByPage(routeManifest, page);
const pageManifest = await getPageManifest(page, options);

// The manifest configuration is the default value for the page configuration.
pageDefaultValueKeys.forEach(key => {
if (!(key in pageManifest) && (key in manifest)) {
pageManifest[key] = manifest[key];
}
});

// Set static dataloader to data_prefetch of page.
pageIds.forEach((pageId) => {
if (typeof page === 'string' && dataloaderConfig && dataloaderConfig[pageId]) {
Expand Down
8 changes: 8 additions & 0 deletions packages/plugin-pha/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export interface PageConfig extends FrameConfig {
defaultFrameIndex?: number;
dataPrefetch?: DataPrefetch[];
queryParams?: string;
pullRefresh?: PullRefresh;
}

export type Page = string | PageConfig;
Expand All @@ -134,6 +135,7 @@ export type PHAFrame = Partial<{
background_color: string;
header_position: 'absolute' | 'static';
enable_pull_refresh: boolean;
pull_refresh: boolean;
priority: Priority;
} & Omit<PHAPage, 'frames' | 'default_frame_index'>>;

Expand All @@ -154,6 +156,7 @@ export type PHAPage = Partial<{
path: string;
background_color: string;
enable_pull_refresh: boolean;
pull_refresh: boolean;
priority: Priority;
script: string;
stylesheet: string;
Expand All @@ -168,6 +171,10 @@ export type PHAPage = Partial<{
frames: PHAFrame[];
}>;

type PullRefresh = boolean | {
reload: boolean;
};

export type Manifest = Partial<{
enablePoplayer: boolean;
disableCapture: boolean;
Expand All @@ -181,6 +188,7 @@ export type Manifest = Partial<{
appWorker: AppWorker;
routes: Page[];
enableExpiredManifest: boolean;
pullRefresh?: PullRefresh;
}> & WindowConfig & Record<string, any>;

export type PHAManifest = Partial<{
Expand Down
179 changes: 179 additions & 0 deletions packages/plugin-pha/tests/manifestHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,185 @@ describe('parse manifest', async () => {
expect(manifest?.app_worker?.url).toBe('https://cdn-path.com/pha-worker.js');
});

it('should work with enable pull refresh', async () => {
const phaManifest = {
appWorker: {
url: 'pha-worker.js',
},
pullRefresh: {
reload: false,
},
routes: [
{
path: '/',
name: 'home',
},
{
path: '/about',
name: 'about',
},
{
path: '/',
name: 'frames',
frames: [
{
name: 'frame1',
url: 'https://m.taobao.com',
},
{
name: 'frame2',
url: 'https://m.taobao.com',
},
],
},
],
};
const manifest = await parseManifest(phaManifest, options);
expect(manifest?.pages![0].enable_pull_refresh).toBe(true);
expect(manifest?.pages![1].enable_pull_refresh).toBe(true);
expect(manifest?.pages![2].enable_pull_refresh).toBe(true);
});

it('should work with enable pull refresh', async () => {
const phaManifest = {
appWorker: {
url: 'pha-worker.js',
},
pullRefresh: {
reload: true,
},
routes: [
{
path: '/',
name: 'home',
pullRefresh: {
reload: true,
},
},
{
path: '/about',
name: 'about',

},
{
path: '/',
name: 'frames',
frames: [
{
name: 'frame1',
url: 'https://m.taobao.com',
pullRefresh: {
reload: true,
},
},
{
name: 'frame2',
url: 'https://m.taobao.com',
pullRefresh: {
reload: true,
},
},
],
},
],
};
const manifest = await parseManifest(phaManifest, options);
expect(manifest?.pages![0].pull_refresh).toBe(true);
expect(manifest?.pages![1].pull_refresh).toBe(true);
expect(manifest?.pages![2].frames![0].pull_refresh).toBe(true);
expect(manifest?.pages![2].frames![1].pull_refresh).toBe(true);
});

it('pull refresh of manifest should be default value of page', async () => {
const phaManifest = {
appWorker: {
url: 'pha-worker.js',
},
pullRefresh: {
reload: true,
},
routes: [
{
path: '/',
name: 'home',
},
{
path: '/about',
name: 'about',
},
],
};
const manifest = await parseManifest(phaManifest, options);
expect(manifest?.pages![0].pull_refresh).toBe(true);
expect(manifest?.pages![1].pull_refresh).toBe(true);
});

it('enable pull refresh of manifest should be default value of page', async () => {
const phaManifest = {
appWorker: {
url: 'pha-worker.js',
},
pullRefresh: true,
routes: [
{
path: '/',
name: 'home',
},
{
path: '/about',
name: 'about',
},
],
};
const manifest = await parseManifest(phaManifest, options);
expect(manifest?.pages![0].enable_pull_refresh).toBe(true);
expect(manifest?.pages![1].enable_pull_refresh).toBe(true);
});

it('enable pull refresh of page should cover value of page', async () => {
const phaManifest = {
appWorker: {
url: 'pha-worker.js',
},
pullRefresh: {
reload: true,
},
routes: [
{
path: '/',
name: 'home',
pullRefresh: {
reload: true,
},
},
{
path: '/about',
name: 'about',
},
],
};
const manifest = await parseManifest(phaManifest, options);
expect(manifest?.pages![0].pull_refresh).toBe(true);
expect(manifest?.pages![1].pull_refresh).toBe(true);
});

it('should work with enable pull refresh', async () => {
const phaManifest = {
appWorker: {
url: 'pha-worker.js',
},
routes: [
{
path: '/',
name: 'home',
pullRefresh: true,
},
],
};
const manifest = await parseManifest(phaManifest, options);
expect(manifest?.pages![0].enable_pull_refresh).toBe(true);
});

it('should set document to manifest', async () => {
const phaManifest = {
routes: [
Expand Down