Skip to content

Commit 0023844

Browse files
re-fcursoragent
andcommitted
fix: stop welcome markdown from replacing the open document
Initialize the current path from the URL and ignore stale content/outline responses so a late welcome fetch cannot overwrite the selected file. Co-authored-by: Cursor <[email protected]>
1 parent 654eeb1 commit 0023844

8 files changed

Lines changed: 89 additions & 28 deletions

File tree

‎packages/frontend/src/api.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
export const fetchData = async <T>(url: string, responseType: 'json' | 'text'): Promise<T | null> => {
1+
export const fetchData = async <T>(
2+
url: string,
3+
responseType: 'json' | 'text',
4+
signal?: AbortSignal,
5+
): Promise<T | null> => {
26
try {
3-
const response = await fetch(url);
7+
const response = signal ? await fetch(url, { signal }) : await fetch(url);
48
if (!response.ok) {
59
throw new Error(`HTTP error! status: ${response.status}`);
610
}

‎packages/frontend/src/components/Content/MarkdownContent/MarkdownContent.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ const MarkdownContent: React.FC<MarkdownContentProps> = ({ scrollToId, onDirecto
3333
const loading = contentLoading || fileTreeLoading;
3434

3535
useEffect(() => {
36-
dispatch(fetchContent(currentPath));
36+
const request = dispatch(fetchContent(currentPath));
37+
return () => {
38+
request?.abort?.();
39+
};
3740
}, [dispatch, currentPath]);
3841

3942
useEffect(() => {

‎packages/frontend/src/components/RightPane/Outline.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ const Outline: React.FC<OutlineProps> = ({ filePath, onItemClick, isOpen, onTogg
2121
const { outline, loading, error } = useSelector((state: RootState) => state.outline);
2222

2323
useEffect(() => {
24-
dispatch(fetchOutline(filePath));
24+
const request = dispatch(fetchOutline(filePath));
25+
return () => {
26+
request?.abort?.();
27+
};
2528
}, [dispatch, filePath]);
2629

2730
const handleItemClickWithClose = useCallback((id: string) => {

‎packages/frontend/src/store/slices/contentSlice.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,30 @@ interface ContentState {
77
loading: boolean;
88
error: string | null;
99
scrollPosition: number;
10+
latestRequestId: string | null;
1011
}
1112

1213
const initialState: ContentState = {
1314
content: '',
1415
loading: true,
1516
error: null,
1617
scrollPosition: 0,
18+
latestRequestId: null,
1719
};
1820

21+
const encodePath = (path: string): string =>
22+
path.split('/').map(encodeURIComponent).join('/');
23+
24+
export const markdownApiUrl = (path: string | null): string => (
25+
path
26+
? `/api/markdown/${encodePath(path)}`
27+
: '/api/markdown/mdts-welcome-markdown.md'
28+
);
29+
1930
export const fetchContent = createAsyncThunk(
2031
'content/fetchContent',
21-
async (path: string | null) => {
22-
const url = path ? `/api/markdown/${path}` : '/api/markdown/mdts-welcome-markdown.md';
23-
const data = await fetchData<string>(url, 'text');
32+
async (path: string | null, { signal }) => {
33+
const data = await fetchData<string>(markdownApiUrl(path), 'text', signal);
2434
return data || '';
2535
}
2636
);
@@ -35,17 +45,24 @@ const contentSlice = createSlice({
3545
},
3646
extraReducers: (builder) => {
3747
builder
38-
.addCase(fetchContent.pending, (state) => {
48+
.addCase(fetchContent.pending, (state, action) => {
49+
state.latestRequestId = action.meta.requestId;
3950
if (!state.content) {
4051
state.loading = true;
4152
}
4253
state.error = null;
4354
})
4455
.addCase(fetchContent.fulfilled, (state, action) => {
56+
if (state.latestRequestId !== action.meta.requestId) {
57+
return;
58+
}
4559
state.loading = false;
4660
state.content = action.payload;
4761
})
4862
.addCase(fetchContent.rejected, (state, action) => {
63+
if (state.latestRequestId !== action.meta.requestId || action.meta.aborted) {
64+
return;
65+
}
4966
state.loading = false;
5067
state.error = action.error.message || 'Failed to fetch content';
5168
});

‎packages/frontend/src/store/slices/historySlice.ts‎

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@ interface HistoryState {
66
isDirectory: boolean;
77
}
88

9-
const initialState: HistoryState = {
10-
currentPath: null,
11-
isDirectory: false,
9+
export const parseHistoryFromPathname = (pathname: string): HistoryState => {
10+
const path = pathname.substring(1);
11+
if (path === '') {
12+
return { currentPath: null, isDirectory: false };
13+
}
14+
15+
const fileExtensions = ['.md', '.markdown'];
16+
const isFile = fileExtensions.some((ext) => path.toLowerCase().endsWith(ext));
17+
18+
return { currentPath: decodeURIComponent(path), isDirectory: !isFile };
1219
};
1320

21+
const initialState: HistoryState = typeof window === 'undefined'
22+
? { currentPath: null, isDirectory: false }
23+
: parseHistoryFromPathname(window.location.pathname);
24+
1425
const historySlice = createSlice({
1526
name: 'history',
1627
initialState,
@@ -28,20 +39,8 @@ const historySlice = createSlice({
2839
export const { setHistory } = historySlice.actions;
2940

3041
export const updateHistoryFromLocation = (pathname: string) => (dispatch: AppDispatch): void => {
31-
const getPathFromUrl = () => {
32-
const path = pathname.substring(1);
33-
if (path === '') return { path: null, isDirectory: false };
34-
35-
const fileExtensions = ['.md', '.markdown'];
36-
const isFile = fileExtensions.some((ext) =>
37-
path.toLowerCase().endsWith(ext)
38-
);
39-
40-
return { path: decodeURIComponent(path), isDirectory: !isFile };
41-
};
42-
43-
const { path, isDirectory } = getPathFromUrl();
44-
dispatch(setHistory({ path, isDirectory }));
42+
const { currentPath, isDirectory } = parseHistoryFromPathname(pathname);
43+
dispatch(setHistory({ path: currentPath, isDirectory }));
4544
};
4645

4746
export default historySlice.reducer;

‎packages/frontend/src/store/slices/outlineSlice.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,23 @@ interface OutlineState {
1111
outline: OutlineItem[];
1212
loading: boolean;
1313
error: string | null;
14+
latestRequestId: string | null;
1415
}
1516

1617
const initialState: OutlineState = {
1718
outline: [],
1819
loading: true,
1920
error: null,
21+
latestRequestId: null,
2022
};
2123

2224
export const fetchOutline = createAsyncThunk(
2325
'outline/fetchOutline',
24-
async (path: string | null) => {
26+
async (path: string | null, { signal }) => {
2527
const url = path
2628
? `/api/outline?filePath=${encodeURIComponent(path)}`
2729
: '/api/outline?filePath=mdts-welcome-markdown.md';
28-
const data = await fetchData<OutlineItem[]>(url, 'json');
30+
const data = await fetchData<OutlineItem[]>(url, 'json', signal);
2931
return data || [];
3032
}
3133
);
@@ -36,15 +38,22 @@ const outlineSlice = createSlice({
3638
reducers: {},
3739
extraReducers: (builder) => {
3840
builder
39-
.addCase(fetchOutline.pending, (state) => {
41+
.addCase(fetchOutline.pending, (state, action) => {
42+
state.latestRequestId = action.meta.requestId;
4043
state.loading = true;
4144
state.error = null;
4245
})
4346
.addCase(fetchOutline.fulfilled, (state, action) => {
47+
if (state.latestRequestId !== action.meta.requestId) {
48+
return;
49+
}
4450
state.loading = false;
4551
state.outline = action.payload;
4652
})
4753
.addCase(fetchOutline.rejected, (state, action) => {
54+
if (state.latestRequestId !== action.meta.requestId || action.meta.aborted) {
55+
return;
56+
}
4857
state.loading = false;
4958
state.error = action.error.message || 'Failed to fetch outline';
5059
});

‎packages/frontend/test/unit/store/contentSlice.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('contentSlice', () => {
1010
loading: true,
1111
error: null,
1212
scrollPosition: 0,
13+
latestRequestId: null,
1314
};
1415

1516
it('should return the initial state', () => {
@@ -23,6 +24,7 @@ describe('contentSlice', () => {
2324
...initialState,
2425
loading: true,
2526
content: '',
27+
latestRequestId: 'requestId',
2628
});
2729
});
2830

@@ -36,6 +38,7 @@ describe('contentSlice', () => {
3638
...initialState,
3739
loading: false,
3840
content: 'old content',
41+
latestRequestId: 'requestId',
3942
});
4043
});
4144
});
@@ -45,11 +48,13 @@ describe('contentSlice', () => {
4548
...initialState,
4649
content: 'old content',
4750
loading: true,
51+
latestRequestId: 'requestId',
4852
};
4953
expect(contentReducer(previousState, fetchContent.fulfilled('new content', 'requestId', null))).toEqual({
5054
...initialState,
5155
content: 'new content',
5256
loading: false,
57+
latestRequestId: 'requestId',
5358
});
5459
});
5560

@@ -58,16 +63,31 @@ describe('contentSlice', () => {
5863
...initialState,
5964
content: 'old content',
6065
loading: true,
66+
latestRequestId: 'requestId',
6167
};
6268
const error = new Error('Failed to fetch');
6369
expect(contentReducer(previousState, fetchContent.rejected(error, 'requestId', null))).toEqual({
6470
...initialState,
6571
content: 'old content',
6672
loading: false,
6773
error: 'Failed to fetch',
74+
latestRequestId: 'requestId',
6875
});
6976
});
7077

78+
it('should ignore stale fetchContent.fulfilled results', () => {
79+
const previousState = {
80+
...initialState,
81+
content: 'current file',
82+
loading: false,
83+
latestRequestId: 'newer-request',
84+
};
85+
expect(contentReducer(
86+
previousState,
87+
fetchContent.fulfilled('# Welcome to mdts! 🚀', 'older-request', null),
88+
)).toEqual(previousState);
89+
});
90+
7191
it('should handle setScrollPosition', () => {
7292
const previousState = { ...initialState };
7393
expect(contentReducer(previousState, setScrollPosition(100))).toEqual({

‎packages/frontend/test/unit/store/outlineSlice.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('outlineSlice', () => {
1010
outline: [],
1111
loading: true,
1212
error: null,
13+
latestRequestId: null,
1314
});
1415
});
1516

@@ -23,6 +24,7 @@ describe('outlineSlice', () => {
2324
outline: [{ id: '1', content: 'old outline', level: 1 }],
2425
loading: true,
2526
error: null,
27+
latestRequestId: 'requestId',
2628
});
2729
});
2830

@@ -31,12 +33,14 @@ describe('outlineSlice', () => {
3133
outline: [{ id: '1', content: 'old outline', level: 1 }],
3234
loading: true,
3335
error: null,
36+
latestRequestId: 'requestId',
3437
};
3538
const newOutline = [{ id: '2', content: 'new outline', level: 1 }];
3639
expect(outlineReducer(previousState, fetchOutline.fulfilled(newOutline, 'requestId', null))).toEqual({
3740
outline: newOutline,
3841
loading: false,
3942
error: null,
43+
latestRequestId: 'requestId',
4044
});
4145
});
4246

@@ -45,12 +49,14 @@ describe('outlineSlice', () => {
4549
outline: [{ id: '1', content: 'old outline', level: 1 }],
4650
loading: true,
4751
error: null,
52+
latestRequestId: 'requestId',
4853
};
4954
const error = new Error('Failed to fetch');
5055
expect(outlineReducer(previousState, fetchOutline.rejected(error, 'requestId', null))).toEqual({
5156
outline: [{ id: '1', content: 'old outline', level: 1 }],
5257
loading: false,
5358
error: 'Failed to fetch',
59+
latestRequestId: 'requestId',
5460
});
5561
});
5662
});

0 commit comments

Comments
 (0)