Skip to content

Commit ca4692a

Browse files
authored
WE-410: relative image links breaking on initial load (#649)
* Fixes the image error, adds some tests and fixes a unique key error I was getting sometimes. * Updates the example media path. * Removes an old attempt at fixing the problem.
1 parent 05cd220 commit ca4692a

File tree

8 files changed

+57
-10
lines changed

8 files changed

+57
-10
lines changed

components/markdown/image.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import { Box } from '@sparkpost/matchbox';
66
const getImageSrc = (asPath: string, src?: string) => {
77
const environment = getWindow();
88
const parts = asPath.split('/');
9-
const dir = parts.splice(0, parts.length - 1).join('/');
9+
const dirArr = parts.splice(0, parts.length - 1);
10+
dirArr.pop();
11+
const dir = dirArr.join('/');
1012
const path = `${environment?.location.origin || 'https://localhost:3000'}/content${dir}/${src}`;
1113
return path;
1214
};

components/site/docsIndexListPageContent.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from 'react';
1+
import React, { useState } from 'react';
22
import Link from 'next/link';
33
import { Box, Tag, Button } from '@sparkpost/matchbox';
44
import { KeyboardArrowDown } from '@sparkpost/matchbox-icons';
@@ -16,9 +16,9 @@ const DocsIndexListPageContent = (props: DocsIndexListPageContentProps) => {
1616
return (
1717
<>
1818
{navigationData && navigationData.items ? (
19-
navigationData.items.slice(0, paginationCount).map((item) => {
19+
navigationData.items.slice(0, paginationCount).map((item, i) => {
2020
return (
21-
<>
21+
<React.Fragment key={`navDataItem${i}`}>
2222
<Box px="500" py="650">
2323
<Box as="h3">{item.title}</Box>
2424
{item.lastUpdated && (
@@ -40,7 +40,7 @@ const DocsIndexListPageContent = (props: DocsIndexListPageContentProps) => {
4040
</Box>
4141
</Box>
4242
<hr />
43-
</>
43+
</React.Fragment>
4444
);
4545
})
4646
) : (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
lastUpdated: '02/08/2020'
3+
title: 'Testing Images'
4+
description: 'This is to test images.'
5+
---
6+
7+
In order to have a working test, you have to maintain the same directory path of the image or else the image build process won't work.
8+
9+
Testing Image 1 without ./
10+
11+
![Billing plan selection without ./](media/upgrading-your-account/billing-page.png)
12+
13+
Testing Image 2 with ./
14+
15+
![Plan upgrade form with ./](./media/upgrading-your-account/upgrade-page.png)

cypress/content/momentum/1st-level/test.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,11 @@ You can link to external resources using this format: `[text here](link here)`.
5858

5959
### Images
6060

61-
Here's an image (put those in the `media` folder for the category in the root `public/content/` directory). For this example, the image path would be in `public/content/example/media/example.png`. This is because next.js requires that images be contained in `public/`:
61+
Here's an example of how an image should be written (put those in the `media` folder for the category in the root `public/content/` directory). For this example, the image path would be in `public/content/momentum/media/test/testing-image.png`. This is because next.js requires that images be contained in `public/`. Creating the link can contain `./` or just `media/`:
6262

63-
![Alternative text here](example/media/example.png)
63+
![Without ./](media/test/testing-image.png)
64+
65+
![With ./](./media/test/testing-image.png)
6466

6567
### Code
6668

cypress/integration/subpage.spec.ts

+21-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('Sub section page', () => {
55
});
66
});
77

8-
describe('test page in subsection', () => {
8+
describe('test page in sub section', () => {
99
it('Should display without error', () => {
1010
cy.visit('/momentum/1st-level/test');
1111
});
@@ -36,3 +36,23 @@ describe('Sub section page', () => {
3636
});
3737
});
3838
});
39+
40+
describe('images', () => {
41+
it('Should display without error without ./', () => {
42+
cy.visit('/docs/billing/upgrading-your-account/');
43+
cy.get('[alt="Billing plan selection without ./"]').should(($img) => {
44+
const img = $img.get(0) as HTMLImageElement;
45+
const width = img.naturalWidth;
46+
expect(width).to.be.greaterThan(0);
47+
});
48+
});
49+
50+
it('Should display without error with ./', () => {
51+
cy.visit('/docs/billing/upgrading-your-account/');
52+
cy.get('[alt="Plan upgrade form with ./"]').should(($img) => {
53+
const img = $img.get(0) as HTMLImageElement;
54+
const width = img.naturalWidth;
55+
expect(width).to.be.greaterThan(0);
56+
});
57+
});
58+
});

example/article.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,19 @@ This is an ordered list:
5050

5151
You can link to external resources using this format: `[text here](link here)`. For example:
5252

53+
- [Duck, duck, GO! (quick check)](https://duckduckgo.com/)
5354
- [SparkPost website](https://www.sparkpost.com)
5455
- [SparkPost API docs](https://developers.sparkpost.com/api)
56+
- [Relative to current directory Link](./relative-link-test)
57+
- [Internal Link](/momentum/1st-level)
5558

5659
### Images
5760

58-
Here's an image (put those in the `media` folder for the category):
61+
Here's an example of how an image should be written (put those in the `media` folder for the category in the root `public/content/` directory). For this example, the image path would be in `public/content/momentum/media/test/testing-image.png`. This is because next.js requires that images be contained in `public/`. Creating the link can contain `./` or just `media/`:
5962

60-
![Alternative text here](example/media/example.png)
63+
![Without ./](media/test/testing-image.png)
64+
65+
![With ./](./media/test/testing-image.png)
6166

6267
### Code
6368

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"predev": "npm run build:images",
77
"prebuild": "npm run build:images",
8+
"prebuild:test": "npm run build:images",
89
"dev": "next dev",
910
"build": "next build",
1011
"build:ssr": "next build && next export",

utils/string.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { NextRouter } from 'next/router';
2+
13
/**
24
* Slugifies a string
35
*/

0 commit comments

Comments
 (0)