Skip to content

Commit c314912

Browse files
committed
refactor: sort env variables .env.example, rename to-internal-uri.ts to to-frontend-uri.ts, update logic for isInternalUrl
1 parent db7616c commit c314912

File tree

9 files changed

+69
-75
lines changed

9 files changed

+69
-75
lines changed

.env.example

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# Enable if connecting to a self-signed cert
22
# NODE_TLS_REJECT_UNAUTHORIZED=0
33

4+
# Token used for authenticating GraphQL introspection queries
5+
INTROSPECTION_TOKEN=
6+
7+
# The CORS proxy prefix to use when bypassing CORS restrictions from WordPress server, Default: /proxy, This means for script module next app will make request NEXT_PUBLIC_FRONTEND_URL/proxy/{module-path}
8+
# NEXT_PUBLIC_CORS_PROXY_PREFIX=/proxy
9+
410
# The headless frontend domain URL
511
NEXT_PUBLIC_FRONTEND_URL=http://localhost:3000
612

7-
NEXT_PUBLIC_WP_SITE_URL=https://headless-demo.local
8-
9-
# The WordPress "frontend" domain URL
10-
NEXT_PUBLIC_WP_HOME_URL=https://headless-demo.local
13+
# Enable if you want to bypass CORS restrictions from WordPress server note: this feature is to bypass CORS restrictions for script modules. Default: process.env.NODE_ENV === 'development'
14+
# NEXT_PUBLIC_USE_CORS_PROXY=false
1115

1216
# The WordPress GraphQL endpoint
1317
NEXT_PUBLIC_WP_GRAPHQL_ENDPOINT=graphql
1418

15-
# The WordPress Uploads directory path
16-
# NEXT_PUBLIC_WP_UPLOADS_PATH=/wp-content/uploads
19+
# The WordPress "frontend" domain URL e.g. https://my-headless-site.local
20+
NEXT_PUBLIC_WP_HOME_URL=
1721

1822
# The WordPress REST URL Prefix
1923
# NEXT_PUBLIC_WP_REST_URL_PREFIX=/wp-json
2024

21-
# Token used for authenticating GraphQL introspection queries
22-
INTROSPECTION_TOKEN=
23-
24-
# Enable if you want to bypass CORS restrictions from WordPress server note: this feature is to bypass CORS restrictions for script modules. Default: process.env.NODE_ENV === 'development'
25-
# NEXT_PUBLIC_USE_CORS_PROXY=false
25+
# NEXT_PUBLIC_WP_SITE_URL=
2626

27-
# The CORS proxy prefix to use when bypassing CORS restrictions from WordPress server, Default: /proxy, This means for script module next app will make request NEXT_PUBLIC_FRONTEND_URL/proxy/{module-path}
28-
# NEXT_PUBLIC_CORS_PROXY_PREFIX=/proxy
27+
# The WordPress Uploads directory path
28+
# NEXT_PUBLIC_WP_UPLOADS_PATH=/wp-content/uploads

examples/nextjs/starter/.env.example

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# Enable if connecting to a self-signed cert
22
# NODE_TLS_REJECT_UNAUTHORIZED=0
33

4+
# Token used for authenticating GraphQL introspection queries
5+
INTROSPECTION_TOKEN=
6+
7+
# The CORS proxy prefix to use when bypassing CORS restrictions from WordPress server, Default: /proxy, This means for script module next app will make request NEXT_PUBLIC_FRONTEND_URL/proxy/{module-path}
8+
# NEXT_PUBLIC_CORS_PROXY_PREFIX=/proxy
9+
410
# The headless frontend domain URL
511
NEXT_PUBLIC_FRONTEND_URL=http://localhost:3000
612

7-
NEXT_PUBLIC_WP_SITE_URL=https://headless-demo.local
8-
9-
# The WordPress "frontend" domain URL
10-
NEXT_PUBLIC_WP_HOME_URL=https://headless-demo.local
13+
# Enable if you want to bypass CORS restrictions from WordPress server note: this feature is to bypass CORS restrictions for script modules. Default: process.env.NODE_ENV === 'development'
14+
# NEXT_PUBLIC_USE_CORS_PROXY=false
1115

1216
# The WordPress GraphQL endpoint
1317
NEXT_PUBLIC_WP_GRAPHQL_ENDPOINT=graphql
1418

15-
# The WordPress Uploads directory path
16-
# NEXT_PUBLIC_WP_UPLOADS_PATH=/wp-content/uploads
19+
# The WordPress "frontend" domain URL e.g. https://my-headless-site.local
20+
NEXT_PUBLIC_WP_HOME_URL=
1721

1822
# The WordPress REST URL Prefix
1923
# NEXT_PUBLIC_WP_REST_URL_PREFIX=/wp-json
2024

21-
# Token used for authenticating GraphQL introspection queries
22-
INTROSPECTION_TOKEN=
23-
24-
# Enable if you want to bypass CORS restrictions from WordPress server note: this feature is to bypass CORS restrictions for script modules. Default: process.env.NODE_ENV === 'development'
25-
# NEXT_PUBLIC_USE_CORS_PROXY=false
25+
# NEXT_PUBLIC_WP_SITE_URL=
2626

27-
# The CORS proxy prefix to use when bypassing CORS restrictions from WordPress server, Default: /proxy, This means for script module next app will make request NEXT_PUBLIC_FRONTEND_URL/proxy/{module-path}
28-
# NEXT_PUBLIC_CORS_PROXY_PREFIX=/proxy
27+
# The WordPress Uploads directory path
28+
# NEXT_PUBLIC_WP_UPLOADS_PATH=/wp-content/uploads

packages/core/src/config/snapwp-config-manager.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,17 @@ const defaultConfig: Partial< SnapWPEnv & SnapWPConfig > = {
8282
*/
8383
const envConfig = (): Partial< SnapWPEnv > => ( {
8484
/* eslint-disable n/no-process-env -- These are the env variables we want to manage. */
85+
corsProxyPrefix: process.env.NEXT_PUBLIC_CORS_PROXY_PREFIX,
86+
graphqlEndpoint: process.env.NEXT_PUBLIC_WP_GRAPHQL_ENDPOINT,
87+
homeUrl: process.env.NEXT_PUBLIC_WP_HOME_URL,
8588
nextUrl: process.env.NEXT_PUBLIC_FRONTEND_URL,
89+
restUrlPrefix: process.env.NEXT_PUBLIC_WP_REST_URL_PREFIX,
8690
// If `siteUrl` is not provided, use `homeUrl`.
8791
siteUrl:
8892
process.env.NEXT_PUBLIC_WP_SITE_URL ||
8993
process.env.NEXT_PUBLIC_WP_HOME_URL,
90-
homeUrl: process.env.NEXT_PUBLIC_WP_HOME_URL,
91-
graphqlEndpoint: process.env.NEXT_PUBLIC_WP_GRAPHQL_ENDPOINT,
9294
uploadsDirectory: process.env.NEXT_PUBLIC_WP_UPLOADS_PATH,
93-
restUrlPrefix: process.env.NEXT_PUBLIC_WP_REST_URL_PREFIX,
9495
useCorsProxy: process.env.NEXT_PUBLIC_USE_CORS_PROXY === 'true',
95-
corsProxyPrefix: process.env.NEXT_PUBLIC_CORS_PROXY_PREFIX,
9696
/* eslint-enable n/no-process-env -- Rule restored. */
9797
} );
9898

packages/core/src/utils/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ export {
1717
removeLeadingSlash,
1818
} from './url-slash-modify';
1919
export { isWPSiteUrl, isWPHomeUrl, isInternalUrl } from './url-checker';
20-
export { toInternalUrl } from './to-internal-uri';
20+
export { toFrontendUri } from './to-frontend-uri';

packages/core/src/utils/tests/to-internal-uri.test.ts packages/core/src/utils/tests/to-frontend-uri.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { addTrailingSlash, toInternalUrl } from '@/utils';
1+
import { addTrailingSlash, toFrontendUri } from '@/utils';
22
import { getConfig } from '@/config';
33

4-
describe( 'toInternalUrl', () => {
4+
describe( 'toFrontendUri', () => {
55
it( 'should return the original URL if it is not an internal URL', () => {
66
const url = 'http://external.com';
7-
expect( toInternalUrl( url ) ).toBe( url );
7+
expect( toFrontendUri( url ) ).toBe( url );
88
} );
99

1010
it( 'should convert a relative internal URL to an absolute URL', () => {
1111
const url = '/path';
1212
const { nextUrl } = getConfig();
13-
expect( toInternalUrl( url ) ).toBe(
13+
expect( toFrontendUri( url ) ).toBe(
1414
new URL( url, nextUrl ).toString()
1515
);
1616
} );
@@ -19,7 +19,7 @@ describe( 'toInternalUrl', () => {
1919
const url = 'https://env-home.example.com/path?query=1#hash';
2020
const { nextUrl } = getConfig();
2121

22-
expect( toInternalUrl( url ) ).toBe(
22+
expect( toFrontendUri( url ) ).toBe(
2323
addTrailingSlash( nextUrl ) + 'path?query=1#hash'
2424
);
2525
} );
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { getConfig } from '@/config';
2+
import { isInternalUrl, isWPHomeUrl } from '@/utils/url-checker';
3+
import { addLeadingSlash } from '@/utils/url-slash-modify';
4+
5+
/**
6+
* Convert a URL to an internal URL.
7+
*
8+
* @param url The URL to convert.
9+
*
10+
* @return The internal URL.
11+
*/
12+
export const toFrontendUri = ( url: string ): string => {
13+
if ( ! isInternalUrl( url ) ) {
14+
return url;
15+
}
16+
17+
if ( url.startsWith( '/' ) ) {
18+
return url;
19+
}
20+
21+
const { homeUrl, siteUrl } = getConfig();
22+
23+
if ( isWPHomeUrl( url ) ) {
24+
return addLeadingSlash( url.replace( homeUrl, '' ) );
25+
}
26+
27+
return addLeadingSlash( url.replace( siteUrl, '' ) );
28+
};

packages/core/src/utils/to-internal-uri.ts

-32
This file was deleted.

packages/core/src/utils/url-checker.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import { getConfig } from '@/config';
99
*/
1010
export const isWPHomeUrl = ( url: string ): boolean => {
1111
const { homeUrl } = getConfig();
12-
return (
13-
url.startsWith( '/' ) || url === homeUrl || url.startsWith( homeUrl )
14-
);
12+
return url === homeUrl || url.startsWith( homeUrl );
1513
};
1614

1715
/**
@@ -23,7 +21,7 @@ export const isWPHomeUrl = ( url: string ): boolean => {
2321
*/
2422
export const isWPSiteUrl = ( url: string ): boolean => {
2523
const { siteUrl } = getConfig();
26-
return url.startsWith( siteUrl );
24+
return url === siteUrl || url.startsWith( siteUrl );
2725
};
2826

2927
/**
@@ -34,5 +32,5 @@ export const isWPSiteUrl = ( url: string ): boolean => {
3432
* @return Whether the URL is internal.
3533
*/
3634
export const isInternalUrl = ( url: string ): boolean => {
37-
return isWPHomeUrl( url ) || isWPSiteUrl( url );
35+
return url.startsWith( '/' ) || isWPHomeUrl( url ) || isWPSiteUrl( url );
3836
};

packages/next/src/components/link.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React, {
33
type CSSProperties,
44
type PropsWithChildren,
55
} from 'react';
6-
import { toInternalUrl, isInternalUrl } from '@snapwp/core';
6+
import { toFrontendUri, isInternalUrl } from '@snapwp/core';
77
import { getConfig } from '@snapwp/core/config';
88
import NextLink, { type LinkProps } from 'next/link';
99

@@ -36,7 +36,7 @@ export default function Link( {
3636
const { graphqlEndpoint } = getConfig();
3737

3838
const internalUri = href
39-
? toInternalUrl( href )?.replace( `/${ graphqlEndpoint }`, '' )
39+
? toFrontendUri( href )?.replace( `/${ graphqlEndpoint }`, '' ) // @todo: Remove replace when the graphql endpoint is removed from the pagination links.
4040
: '';
4141

4242
if ( ! isInternalUrl( internalUri ) ) {

0 commit comments

Comments
 (0)